Updates to the Java API
for JSON Processing for
Java EE 8
Alex Soto & Mite Mitreski
#JSON #JAVAEE
Mite Mitreski
Software engineer
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>
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
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.
IETF RFC 7159
The JavaScript Object Notation (JSON)
Data Interchange Format
Tim Bray - Google
2014 - Old new JSON
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)
JsonArray and
JsonObject
Transformations
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
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();
JSON Pointer
IETF RFC 6901
JSON Pointer
IETF RFC 6901
● String syntax for identifying a specific value
○ /author/name
○ /parents/0
● ‘/’ —> ‘~1’
● ‘~’ —> ‘~0’
● Absolute
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"
}
}
JSON Pointer
● Apply operations to a pointer
○ add(JsonStructure, JsonValue)
○ replace(JsonStructure, JsonValue)
○ remove(JsonStructure)
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"
}
DEMO TIME
JSON Patch
IETF RFC 6902
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)
JSON-PATCH — IETF RFC 6902
[
{"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"},
{"op":"remove", "path": "/title"}
]
"title":"Guinness",
"brewery": {
"key": "guinness"
}
"brewery": {
"key": "GuinessBrewery"
}
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);
JSON-PATCH — IETF RFC 6902
JsonPatchBuilder patchBuilder = new JsonPatchBuilder();
JsonStructure newBeer = patchBuilder
.replace("/brewery/key", "GuinessBrewery")
.remove("/title")
.apply(commit);
JSON-PATCH — extra operations
● Diff between two documents
○ Reverse operation
● Useful in case of conflicts
JSON-PATCH — extra operations
JsonArray diff = JsonPatch.diff(beer1, beer2);
[
{"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"},
{"op":"remove", "path": "/title"}
]
DEMO TIME
JSON Merge Patch
IETF RFC 7386
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)
JSON Merge patch
"title":"Guinness",
"brewery": {
"key": "guinness"
}
{"title":null}
"brewery": {
"key": "guinness"
}
JSON Merge patch
JsonStructure beer = reader.read();
JsonObject removeTitle = Json.createObjectBuilder()
.add("title", JsonValue.NULL)
.build();
JsonValue newBeer = JsonMergePatch.mergePatch(beer, removeTitle);
JSON Merge patch
● Diff between two documents
○ Reverse operation
● Useful in case of conflicts
JSON Merge patch
JsonValue diff = JsonMergePatch.diff(beer, newBeer);
{"title": null}
DEMO TIME
JSON Queries
Working with java.util.Stream
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.
JSON queries
● toJsonArray
○ Accumulates values in a JsonArray
● toJsonObject
○ Accumulates values in a JsonObject
● groupBy
○ Implements “group by” operations on the values
DEMO TIME
BIG JSON Data
JSON processing of large objects
JSON-P 1.1 Big JSON
● Too big to build data model in memory
● Dynamically generated JSON
● Usually interested only small fraction of data
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
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
DEMO TIME
Alignment
with JSON-B and JAX-RS
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
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”));
JAX-RS
@Patch
@Consumes(MediaType.APPLICATION_JSON_PATCH)
public Response updateBook(..., JsonPatch patch){}
● integration with JSON-B
● Integration with JSON-P PATCH
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
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
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
QUESTIONS ?
● Alex Soto
○ @alexsotob
● Mite Mitreski
○ @mitemitreski

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

  • 1.
    Updates to theJava API for JSON Processing for Java EE 8 Alex Soto & Mite Mitreski #JSON #JAVAEE
  • 3.
  • 4.
    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>
  • 5.
    Agenda ● IETF RFC7159 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
  • 6.
    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.
  • 7.
    IETF RFC 7159 TheJavaScript Object Notation (JSON) Data Interchange Format Tim Bray - Google 2014 - Old new JSON
  • 8.
    JSON-P 1.1 IETF RFC7159 – 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)
  • 9.
  • 10.
    JSON-P 1.1 ● Transformationon 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
  • 11.
    Example : JSONarray 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();
  • 12.
  • 13.
    JSON Pointer IETF RFC6901 ● String syntax for identifying a specific value ○ /author/name ○ /parents/0 ● ‘/’ —> ‘~1’ ● ‘~’ —> ‘~0’ ● Absolute
  • 14.
    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" } }
  • 15.
    JSON Pointer ● Applyoperations to a pointer ○ add(JsonStructure, JsonValue) ○ replace(JsonStructure, JsonValue) ○ remove(JsonStructure)
  • 16.
    JSON Pointer extraoperations 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" }
  • 17.
  • 18.
  • 19.
    JSON-P 1.1 JSONPATCH ● Standardise Update operation ● Sequence of modifications ○ test, remove, add, replace, move, copy ● JSON PATCH is a document ● HTTP PATCH method (application/json-patch+json)
  • 20.
    JSON-PATCH — IETFRFC 6902 [ {"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"}, {"op":"remove", "path": "/title"} ] "title":"Guinness", "brewery": { "key": "guinness" } "brewery": { "key": "GuinessBrewery" }
  • 21.
    JSON-PATCH — IETFRFC 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);
  • 22.
    JSON-PATCH — IETFRFC 6902 JsonPatchBuilder patchBuilder = new JsonPatchBuilder(); JsonStructure newBeer = patchBuilder .replace("/brewery/key", "GuinessBrewery") .remove("/title") .apply(commit);
  • 23.
    JSON-PATCH — extraoperations ● Diff between two documents ○ Reverse operation ● Useful in case of conflicts
  • 24.
    JSON-PATCH — extraoperations JsonArray diff = JsonPatch.diff(beer1, beer2); [ {"op":"replace", "path":"/brewery/key", “value”:"GuinessBrewery"}, {"op":"remove", "path": "/title"} ]
  • 25.
  • 26.
  • 27.
    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)
  • 28.
    JSON Merge patch "title":"Guinness", "brewery":{ "key": "guinness" } {"title":null} "brewery": { "key": "guinness" }
  • 29.
    JSON Merge patch JsonStructurebeer = reader.read(); JsonObject removeTitle = Json.createObjectBuilder() .add("title", JsonValue.NULL) .build(); JsonValue newBeer = JsonMergePatch.mergePatch(beer, removeTitle);
  • 30.
    JSON Merge patch ●Diff between two documents ○ Reverse operation ● Useful in case of conflicts
  • 31.
    JSON Merge patch JsonValuediff = JsonMergePatch.diff(beer, newBeer); {"title": null}
  • 32.
  • 33.
    JSON Queries Working withjava.util.Stream
  • 34.
    JSON queries ● AJsonObject 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.
  • 35.
    JSON queries ● toJsonArray ○Accumulates values in a JsonArray ● toJsonObject ○ Accumulates values in a JsonObject ● groupBy ○ Implements “group by” operations on the values
  • 36.
  • 37.
    BIG JSON Data JSONprocessing of large objects
  • 38.
    JSON-P 1.1 BigJSON ● Too big to build data model in memory ● Dynamically generated JSON ● Usually interested only small fraction of data
  • 39.
    JSON-P 1.1 BigJSON ● JsonParser parses JSON using streaming model ● Pull model, under user control ● Efficient in execution and memory usage ● Low level, token based
  • 40.
    JSON-P 1.1 BigJSON ● skipArray ○ Skip tokens and advance the parser to END_ARRAY ● skipObject ○ Skip tokens and advance the parser to END_OBJECT
  • 41.
  • 42.
  • 43.
    JSON-B ● API tomarshal/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
  • 44.
    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”));
  • 45.
    JAX-RS @Patch @Consumes(MediaType.APPLICATION_JSON_PATCH) public Response updateBook(...,JsonPatch patch){} ● integration with JSON-B ● Integration with JSON-P PATCH
  • 46.
    JSON-P 1.1 Currentstatus ● 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
  • 47.
    JSON-P 1.1 ExpertGroup ● 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
  • 48.
    JSON-P 1.1 ReferenceImplementation ● 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
  • 49.
    QUESTIONS ? ● AlexSoto ○ @alexsotob ● Mite Mitreski ○ @mitemitreski