Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any...

42
Johnzon - Apache’s Upcoming JSON Library Hendrik Saly, codecentric AG

Transcript of Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any...

Page 1: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Johnzon-Apache’sUpcomingJSONLibraryHendrikSaly,codecentricAG

Page 2: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

AbouttheApacheIncubator"TheIncubatorprojectistheentrypathintoTheApacheSoftwareFoundation(ASF)forprojectsandcodebaseswishingtobecomepartoftheFoundation’sefforts."( )

TheApacheIncubatorhastwoprimarygoals:

EnsurealldonationsareinaccordancewiththeASFlegalstandards

Developnewcommunitiesthatadheretoourguidingprinciples

http://incubator.apache.org/

Page 3: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

WhatisJohnzon?

LightweightJSONlibrarywritteninJava

Core<90k,200kforthewholelibrary

Noexternaldependencies

ImplementationofJSR-353

Apache2License

Page 4: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

WhatisJohnzon?(cont.)

ContainsalsomoduleswhicharenotdefinedinJSR-353

Objectmapper

JAX-RSprovider

Websocket(JSR-356)integration(beta)

JSONDSLformutatingdocumentscomfortably(beta)

Page 5: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Statusoftheproject

Smallbutengaged,responsiveandfriendlycommunity

Stableandproductionready

PerformanceforJohnzoncoreisquitewell

CurrentlyincubatingwithintheApacheIncubator

andlookingfornewcommunitymembers

lowentrancebarrier

Planistograduatesoon(startthisyear)

Page 6: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Roadmap

ImplementJSR-367(APIforJSONBinding/JSON-B)

ImplementJSR-374(UpdateofJSR-353)

Performanceenhancements

Increasetestcoverage

JEEcertification(onceTCKavailable)

Page 7: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

CurrentusersofJohnzon

ApacheTomEE2

ApacheTamaya

ApacheDecanter

Page 8: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JSRThespecpart

Page 9: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

WhatisaJSR

JSR→JavaSpecificationRequest

Aformalstandardizationprocess

Communityinvolved

ControlledbyOraclewithintheJavacommunityprocess(JCP)

Page 10: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JSR-353Basics

CanbeusedstandaloneorwithinaJEEcontainer

ProvidesastreamingAPIaswellasa"TreeModel"APIforparsing

GeneratorAPIforgeneratingvalidJSONstreams

Page 11: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

StreamingAPI

Pullparser,parseJSONfilesofanysize

Encodingautodetection

JsonParser parser = Json.createParser(new FileReader("file.json"));while(parser.hasNext() { Event event = parser.next(); if(event == Event.VALUE_STRING) { System.out.println(parser.getString()); }}

Page 12: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Encodingautodetection

RFC4627Chapter3

"JSONtextSHALLbeencodedinUnicode"

FirsttwocharacterarealwaysASCII

Sowecanusethismatrixtodetectencoding:

(Johnzondoesalsohandleoctet-streamswithBOM’scorrectly)

00 00 00 xx UTF-32BE00 xx 00 xx UTF-16BExx 00 00 00 UTF-32LExx 00 xx 00 UTF-16LExx xx xx xx UTF-8

Page 13: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

"TreeModel"API

ParseJSONtoimmutableobjecttree

Caution:Allin-memory

JsonReader reader = Json.createReader(new StringReader("[]"));JsonArray array = reader.readArray();System.out.println(array.isEmpty());System.out.println(array.get(0));jsonReader.close();

Page 14: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

GeneratorAPI

WriteJSON"valuebyvalue"intoabyte/charstream

Writer writer = ...;JsonGenerator generator = Json.createGenerator(writer);generator .writeStartObject() .write("firstName", "Mister") .write("lastName", "Spock") .write("age", 99) .writeStartObject("address") .write("streetAddress", "Kolinahr Street 1") .write("city", "Vulcan City") .write("state", "VU") .write("postalCode", "1701") .writeEnd() .writeEnd();generator.close();

Page 15: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

WriterAPI

Writethe"TreeModel"backintoabyte/charstream

Outputstream out = ...;JsonObject jo = ...;JsonWriter jsonWriter = Json.createWriter(out);jsonWriter.writeObject(jo);jsonWriter.close();

Page 16: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Configuration

Key-Valuebased

Implementationdependent

Viafactories

final JsonReader reader = Json.createReaderFactory(new HashMap<String, Object>() { put("org.apache.johnzon.supports-comments", true); }).createReader(...);JsonParser generator = Json.createGeneratorFactory();JsonGenerator generator = Json.createGeneratorFactory();

Page 17: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Johnzoninparticular

Page 18: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JohnzonnonJSR-353Features

Comments(singleline/multiline)

Configurablebuffersizes

Differentbufferreusestrategies

QUEUE-char[]arereusedbyConcurrentLinkedQueue(default)

BY_INSTANCE-char[]arenotreused

SINGLETON-char[]arereusedbyonlyoneglobalchar[]

THREAD_LOCAL-char[]arereusedbythread(everythreaddoeshaveitschar[]bufferboundtoathreadlocal)

Page 19: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

The"Mapper"

JSON←→JavaBinding

UsedbyJAXRSprovider/Websocketmodule

Supports

Customde-/serializers

Properhandlingofcollectionsandgenerics

@JohnzonConverterand@JohnzonIgnoreannotations

Workswithfields,getter/setterorboth

Configurablenull/emptyhandling

Configurablebyte[]handling

Page 20: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

The"Mapper"(cont.)

(YetsomemissingdefaultdatatypesforJavaSE8likejava.time.*)

final static Mapper mapper = new MapperBuilder().build();MyObject myObj = ...;mapper.writeObject(myObj, outputStream);MyObject myObj2 = mapper.readObject(inputStream, MyObject.class);

Page 21: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

The"Mapper"(cont.)

Worksmostlyhasslefree

WillbealignedwithJSON-BSpec(JSR-367)

Needsomeperformancetuning

Page 22: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JAX-RSProvider<Service id="johnzon" class-name="org.apache.johnzon.jaxrs.ConfigurableJohnzonProvider"> ignores = com.foo.MyType,com.foo.MyOtherType accessMode = method supportHiddenAccess = true doCloseOnStreams = false version = 2 skipNull = true skipEmptyArray = true</Service>

Page 23: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Websockets

SinceJohnzon0.8thereisaWebsocket/JSR-356integration

JSONaspayloadformatforWebSocketmessages

https://rmannibucau.wordpress.com/2015/03/24/json-and-websocket-johnzon-to-the-rescue/

Page 24: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Websockets(cont.)Server

Client

@ServerEndpoint(value = "/server", encoders = JohnzonTextEncoder.class, decoders = JohnzonTextDecoder.class)public class MyServerEndpoint { // same as before}

public class MessageDecoder extends JohnzonTextDecoder { public MessageDecoder() { super(Message.class); }}// and used like:@ClientEndpoint(encoders = JohnzonTextEncoder.class, decoders = MessageDecoder.class)public class ClientEndpointImpl { // ...}

Page 25: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JohnzonDSL

UpcomingwithJohnzon2

MutableandnavigableJSONStructure

FluentAPI

Page 26: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JohnzonDSL(cont.)

Lookslike:

JsonObject jo = ...;MutableJsonStructure ms = MutableJsonStructureFactory.toMutableJsonStructure(jo);assertNotSame(ms, ms.copy());assertFalse(ms.isLeaf("address"));assertFalse(ms.isLeafNull("firstName"));assertTrue(ms.exists("phoneNumber"));assertEquals(1, ms.get("phoneNumber").get(1).getAncestor().getIndex());assertNull(ms.getParent());assertEquals("Smith", ms.getLeafAsString("lastName"));assertEquals("NY", ms.get("address").getLeafAsString("state"));assertEquals(5, ms.getKeys().size());assertEquals(5, ms.size());assertEquals(4, ms.get("address").size());ms.add("additionalAddress", ms.get("address").copy().remove("city").set("state", "CA"));ms.set(ms.copy().remove("phoneNumber"));assertEquals(5, ms.size());JsonObject modJo = (JsonObject) ms.toJsonStructure();

Page 27: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Benchmark

JMHbasedbenchmarksuite

Bytes,Chars(UTF-8/UTF-16)

Measurements

ParseOnly

Readto"TreeModel"

GenerateJSON

Serialize

Deserialize

Page 28: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Benchmark(cont.)

SmallsizeJSON

Page 29: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Benchmark(cont.)

MediumsizeJSON(bytestream)

Page 30: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Benchmark(cont.)

MediumsizeJSON(characterstream)

Page 31: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Benchmark(cont.)

SerializesimpleJavaObject

Page 32: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Testcoverage

Approx.74%testcoverageyet

Wewanttogetabove90%

https://coveralls.io/github/salyh/incubator-johnzon

Page 33: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

UpcomingThenewJSONJSRSpecs

Page 34: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JSR-374(JSON-P1.1)

RFC7159(updateofRFC4627)

JavaSE8

JsonPointer(RFC6901)

JsonPatch(RFC6902)

JsonMergePatch(RFC7396)

Mutable"TreeModel"

CurrentlyEDR(Earlydraftreview)

Page 35: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JSR-367(JSON-B1.0)

JavastandardforJSON<→JavaBinding

JavaSE8

IntegrateswithJSR-353/374

CurrentlyEDR(Earlydraftreview)

Page 36: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

JSR-367(JSON-B1.0)(cont.)Jsonb jsonb = JsonbBuilder.create();Book book = jsonb.fromJson(new File("jsonfile.json"), Book.class);

Page 37: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Gettingstartedandinvolved

Page 38: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Getitfrommavencentral

ordownloadfrom

<dependency> <groupId>org.apache.johnzon</groupId> <artifactId>johnzon-core</artifactId> <version>0.9.1-incubating</version></dependency>

<dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-json_1.0_spec</artifactId> <version>1.0-alpha-1</version> <scope>provided</scope> <!-- or compile if your environment doesn't provide it --></dependency>

http://www.eu.apache.org/dist/incubator/johnzon/

Page 39: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Wheretogofromhere

http://incubator.apache.org/projects/johnzon.html

Subscribetothedevmailinglist

https://github.com/salyh/jsr353-benchmark

https://github.com/apache/incubator-johnzon/tree/jsr374_367

https://www.jcp.org/en/jsr/detail?id=353

https://www.jcp.org/en/jsr/detail?id=374

https://www.jcp.org/en/jsr/detail?id=367

Page 40: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Considertojointheprojectifyouare

a(Java)developerlookingtogetinvolvedwithinASF

interestedinimplementingstandards

doingJSONthewholeday

lookingforagreatcommunitytoengagewith

Page 41: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Github

WeacceptPullRequests

https://github.com/apache/incubator-johnzon

Page 42: Johnzon Apache’s Upcoming JSON Library · 2017-12-14 · Pull parser, parse JSON files of any size Encoding autodetection JsonParser parser = Json.createParser(new FileReader("file.json"));

Thankyou!

FollowmeonTwitter:

Thisworkislicensedundera

[email protected]

@hendrikdev22

CreativeCommonsAttribution4.0InternationalLicense