JSR 374: Java API for JSON Processing 1.1

Bogdan Posa,

BJUG, 28.01.2017

JSON PROCESSING

Manipulate JSON using:

          - Object Model API

          - Streaming APIs

Object Model API -> tree-like structure that represents the JSON data in memory

The streaming API -> parse and generate JSON in a streaming fashion

What is JSON-P/JSON-P 1.1

 

Java EE 7 came with JSR353 = JSON-P

Java EE 8 will come with JSR374 = JSON-P 1.1

- Java 8 (streams)

- JSON Pointer

- JSON Patch

- JSON Merge Patch

- Impact on REST APIs

 

JSON Pointer - RFC 6901

defines a string syntax for identifying a specific value within JSON document.

zero-to-many tokens, each prefixed by "/"

JSON Pointer - RFC 6901

{
    "foo" : ["bar", "baz"],
    "pi" : 3.1416
}

"/foo" → ["bar", "baz"]

"/foo/0" → "bar"

"/foo/1" → "baz"

"/pi" → 3.1416

 

JSON Pointer - RFC 6901

JsonPointer p = Json.createPointer("/author/name");

JsonPointer#getValue
JsonPointer#remove
JsonPointer#replace
JsonPointer#add

JSON PATCH - RFC 6902

JSON document that contains a sequence of modifications, which they are executed all of them or none of them

Operations: test, remove, add, replace, move and copy

Uses JSON Pointer to specify the part of the document to operate on

JSON PATCH

[
     { "op": "test", "path": "/a/b/c", "value": "foo" },
     { "op": "remove", "path": "/a/b/c" },
     { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
     { "op": "replace", "path": "/a/b/c", "value": 42 },
     { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
     { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
]

JSON PATCH

{
  "title":"Guinness",
  "brewery": {
      "key": "guinness"
  }
}
[
  {"op":"replace", "path":"/brewery/key", “value”:"GBrewery"},
  {"op":"remove", "path": "/title"}
]
{
  "brewery": {
    "key": "GBrewery"
  }
}

JSON Merge Patch - RFC 7386

Describes changes to be made to a target JSON document using a syntax that closely mimics the document being modified.

{
"firstName":"Ion"
}
{
"firstName":"Ion",
"lastName":null
}
{
"firstName":"Posa",
"lastName":"Bogdan"
}

JSON Stream Processing

Process a JSON object the same way you process Java 8 streams

jsonArray.getValuesAs(JsonObject.class).stream()
             .filter(x -> x.getString("firstName").length() == 4)
             .count()

Give me the code

(I didn't found other images  :) )

Big JSON Data

Main classes:

          - JsonParser

JsonParser parser = Json.createParser(inputStream);
JsonParser#next():Event

- JsonGenerator

JsonGenerator jsonGenerator = jsonGeneratorFactory.createGenerator(fos);

JsonGenerator#writeStartArray()
JsonGenerator#write(JsonValue)
JsonGenerator#writeEnd()
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonArray doGet()
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void doPost(JsonArray structure)

ALIGNMENT WITH JAX-RS

ALIGNMENT WITH JAX-RS

@Patch
@Consumes(MediaType.APPLICATION_JSON_PATCH)
public Response updateBook(JsonPatch patch){}
@Patch
@Consumes(MediaType.APPLICATION_JSON_MERGE_PATCH)
public Response updateBook(JsonObject mergePatch
PATCH /my/data HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json
PATCH /my/resource HTTP/1.1
Host: example.org
Content-Type: application/merge-patch+json

- Read the spec (https://json-processing-spec.java.net/)

- Read the IETF specs for Json Pointer, Json Patch, Json Merge Patch

- Play with RI and give feedback  - https://github.com/json-p/api-ri

WAIT FOR JAVA EE 8 - IT WILL BE AWESOME

Recommendations/Conclusions

THANKS

 

 

LET'S PLAY !!!

JSON PROCESSING

By Bogdan Posa

JSON PROCESSING

  • 1,456