Updates to the java api for json processing for java ee 8

2,485 views

Published on

One of the additions of Java EE 7 was the JSON-P specification for processing JSON documents. But JSON-P spec is moving forward and for Java EE 8 it will come with a lot of new features like support for JSON Pointer (RFC6901), JSON Patch (RFC6902) or JSON Merge Patch (RFC7386). But also improvements on performance, Java 8 integration and how to process big JSON data.

The first part of this session presents the JSON Processing API that comes with Java EE 7 to understand the basis for improvements of next version. The second part of the session presents the updates that are coming on the Java API for JSON Processing that they will be added inside Java EE 8.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,485
On SlideShare
0
From Embeds
0
Number of Embeds
1,248
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Updates to the java api for json processing for java ee 8

  1. 1. Updates to the Java API for JSON Processing for Java EE 8 Alex Soto & Mite Mitreski #JSON #JAVAEE
  2. 2. Mite Mitreski Software engineer
  3. 3. JSON-P 1.0 ● JSR-353 ● javax.json ○ Json, JsonReader,JsonWriter JsonString, JsonNumber... ● Implementations ○ RI in GlassFish ○ Jonzon ○ Genson <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.0.4</version> </dependency>
  4. 4. Agenda ● IETF RFC 7159 aka new JSON ● JsonArray and JsonObject transformations ● JSON Pointer ● JSON Patch, JSON Merge Patch ● JSON Queries ● Big JSON Processing ● Alignment with JSON-B ● Alignment with JAX-RS
  5. 5. RFC - 4627 - JSON - The application/json Media Type for JavaScript Object Notation July 2006 JSON.org On the 8th day, Douglas Crockford introduced JSON. And he saw that it was good.
  6. 6. IETF RFC 7159 The JavaScript Object Notation (JSON) Data Interchange Format Tim Bray - Google 2014 - Old new JSON
  7. 7. JSON-P 1.1 IETF RFC 7159 – New API ● Json ○ public static JsonString createValue(String value) ○ public static JsonNumber createValue(int value) ○ Similarly for long, double, BigInteger, and BigDecimal ● JsonReader ○ default JsonValue readValue() ● JsonWriter ○ default void write(JsonValue value)
  8. 8. JsonArray and JsonObject Transformations
  9. 9. JSON-P 1.1 ● Transformation on immutable JsonArray and JsonObject ● Low level operations used in JsonPatch ● Use builder pattern ○ Create builders with initial JsonArray or JsonObject ○ Add editing operations to builders ○ Builder returns immutable JsonArray or JsonObject when done JsonArray and JsonObject Transformations
  10. 10. Example : JSON array transformation // Create the target JsonArray target = Json.createArrayBuilder().add(…).build(); // Create a builder initialized with the target JsonArrayBuilder builder = Json.createArrayBuilder(target); // Operations on the array JsonArray result = builder.add(99, "john") .set(100, 1234) .remove(3) .build();
  11. 11. JSON Pointer IETF RFC 6901
  12. 12. JSON Pointer IETF RFC 6901 ● String syntax for identifying a specific value ○ /author/name ○ /parents/0 ● ‘/’ —> ‘~1’ ● ‘~’ —> ‘~0’ ● Absolute
  13. 13. JSON Pointer JsonStructure beer = reader.read(); JsonPointer p = new JsonPointer("/brewery/key"); JsonValue name = p.getValue(beer); { "key":"guinness", "title":"Guinness", "brewery": { "key": "guinness", "title": "St. James's Gate Brewery" } }
  14. 14. JSON Pointer ● Apply operations to a pointer ○ add(JsonStructure, JsonValue) ○ replace(JsonStructure, JsonValue) ○ remove(JsonStructure)
  15. 15. JSON Pointer extra operations JsonStructure beer = reader.read(); JsonPointer p = new JsonPointer("/brewery/key"); JsonStructure bewBeer = p.replace(beer, Json.createValue("GuinnessBrewery")); "title":"Guinness", "brewery": { "key": "guinness" } "title":"Guinness", "brewery": { "key": "GuinessBrewery" }
  16. 16. DEMO TIME
  17. 17. JSON Patch IETF RFC 6902
  18. 18. JSON-P 1.1 JSON PATCH ● Standardise Update operation ● Sequence of modifications ○ test, remove, add, replace, move, copy ● JSON PATCH is a document ● HTTP PATCH method (application/json-patch+json)
  19. 19. JSON-PATCH — IETF RFC 6902 [ {"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"}, {"op":"remove", "path": "/title"} ] "title":"Guinness", "brewery": { "key": "guinness" } "brewery": { "key": "GuinessBrewery" }
  20. 20. JSON-PATCH — IETF RFC 6902 JsonArrayBuilder patchExpression = Json.createArrayBuilder(); //creates operation JsonObjectBuilder patch = Json.createObjectBuilder() .add("op", "replace") .add("path", "/brewery/key") .add("value", “GuinessBrewery"); patchExpression.add(patch); JsonPatch jsonPatch = new JsonPatch(patchExpression.build()); JsonStructure beer = reader.read(); JsonStructure newBeer = jsonPatch.apply(beer);
  21. 21. JSON-PATCH — IETF RFC 6902 JsonPatchBuilder patchBuilder = new JsonPatchBuilder(); JsonStructure newBeer = patchBuilder .replace("/brewery/key", "GuinessBrewery") .remove("/title") .apply(commit);
  22. 22. JSON-PATCH — extra operations ● Diff between two documents ○ Reverse operation ● Useful in case of conflicts
  23. 23. JSON-PATCH — extra operations JsonArray diff = JsonPatch.diff(beer1, beer2); [ {"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"}, {"op":"remove", "path": "/title"} ]
  24. 24. DEMO TIME
  25. 25. JSON Merge Patch IETF RFC 7386
  26. 26. JSON Merge patch ● Standardise Update operation ● Syntax that closely mimics the document being modified ● null value to delete ● JSON PATCH is a document ● HTTP PATCH method (application/json-patch+json)
  27. 27. JSON Merge patch "title":"Guinness", "brewery": { "key": "guinness" } {"title":null} "brewery": { "key": "guinness" }
  28. 28. JSON Merge patch JsonStructure beer = reader.read(); JsonObject removeTitle = Json.createObjectBuilder() .add("title", JsonValue.NULL) .build(); JsonValue newBeer = JsonMergePatch.mergePatch(beer, removeTitle);
  29. 29. JSON Merge patch ● Diff between two documents ○ Reverse operation ● Useful in case of conflicts
  30. 30. JSON Merge patch JsonValue diff = JsonMergePatch.diff(beer, newBeer); {"title": null}
  31. 31. DEMO TIME
  32. 32. JSON Queries Working with java.util.Stream
  33. 33. JSON queries ● A JsonObject is a Map, and a JsonArray is a List, so queries can be implemented with Java’s stream operations, using Lambda expressions ● Get concurrent processing for free ● We need collectors that return JsonArray or JsonObject instead of List or Map.
  34. 34. JSON queries ● toJsonArray ○ Accumulates values in a JsonArray ● toJsonObject ○ Accumulates values in a JsonObject ● groupBy ○ Implements “group by” operations on the values
  35. 35. DEMO TIME
  36. 36. BIG JSON Data JSON processing of large objects
  37. 37. JSON-P 1.1 Big JSON ● Too big to build data model in memory ● Dynamically generated JSON ● Usually interested only small fraction of data
  38. 38. JSON-P 1.1 Big JSON ● JsonParser parses JSON using streaming model ● Pull model, under user control ● Efficient in execution and memory usage ● Low level, token based
  39. 39. JSON-P 1.1 Big JSON ● skipArray ○ Skip tokens and advance the parser to END_ARRAY ● skipObject ○ Skip tokens and advance the parser to END_OBJECT
  40. 40. DEMO TIME
  41. 41. Alignment with JSON-B and JAX-RS
  42. 42. JSON-B ● API to marshal/unmarshal Java objects to/from JSON ● From best practices of existing JSON binders a. Gson, Jackson, Johnzon, ... ● Implementations compete on common ground ● MUST support binding of the JSON Processing API
  43. 43. JSON-B Jsonb jsonb = JsonbBuilder.create(); // Unmarshal Book book = jsonb.fromJson(new File(“book.json”), Book.class); // Marshal jsonb.toJson(book, new File(“book.json”));
  44. 44. JAX-RS @Patch @Consumes(MediaType.APPLICATION_JSON_PATCH) public Response updateBook(..., JsonPatch patch){} ● integration with JSON-B ● Integration with JSON-P PATCH
  45. 45. JSON-P 1.1 Current status ● Early Draft Review ○ http://download.oracle.com/otndocs/jcp/json_p-1_1-edr-spec/index.html ● Roadmap ○ Q1 2016 Public Review ○ Q3 2016 Proposed Final Draft ○ H1 2017 Final Release
  46. 46. JSON-P 1.1 Expert Group ● Public Project Page ○ https://java.net/projects/json-processing-spec ● Public Communications ○ https://java.net/projects/json-processing-spec/lists ● Issue Tracking ○ https://java.net/jira/browse/JSON_PROCESSING_SPEC
  47. 47. JSON-P 1.1 Reference Implementation ● Public Project Page ○ https://java.net/projects/jsonp ○ https://jsonp.java.net/ ● Source Code Repository ○ git://java.net/jsonp~git ○ https://github.com/json-processing-inofficial ● Issue Tracking ○ https://java.net/jira/browse/JSONP
  48. 48. QUESTIONS ? ● Alex Soto ○ @alexsotob ● Mite Mitreski ○ @mitemitreski

×