Json - Java.net

Report
JSON Pointer and JSON Patch
Updates to Java API for JSON Processing
Kin-man Chung
Oracle Corporation
September 30, 2014
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
3
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
4
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
5
JSON Object Model Overview
JsonValue
• An immutable representation of a JSON value
• Represented by:
– JsonObject
– JsonArray
– JsonNumber
– JsonString
– Literals
• TRUE, FALSE, and NULL
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
6
JSON Object Model Overview
JsonObject
• Represents a JSON object
• java.util.Map<String, JsonValue>
• Created with
– JsonObjectBuilder
– JsonObjectReader
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
7
JSON Object Model Overview
JsonArray
• Represents a JSON array
• java.util.List<JsonValue>
• Created with
– JsonArrayBuilder
– JsonArrayReader
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
8
JSON Object Model Overview
Json
• Factory class for creating JSON objects
• Plugin factories with service provider
• API includes
– createObjectBuilder()
– createArrayBuilder()
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
9
Example: contacts in JSON
[
]
{
"name": "Duke",
"gender": "M",
"phone": {
"areacode": "650",
"number": "234-5678"}
},
{
"name": "Jane",
”gender": "F",
"phone": {
"areacode": "777",
"number": "999-5555"}
},
{
"name": "Amy",
"gender": "F",
"phone": {
"areacode": "505"
"number": "333-4444"}
}
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
10
Example: contacts in JSON object model
JsonArray contacts = Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("name", "Duke")
.add("gender", "M")
.add("phone", Json.createObjectBuilder()
.add("areacode", "650")
.add("number", "234-5678")))
.add(Json.createObjectBuilder()
.add("name", "Jane")
.add("gender", "F")
.add("phone", Json.createObjectBuilder()
.add("areacode", ”777")
.add("number": "999-5555")))
.add(Json.createObjectBuilder()
.add("name", “Amy")
.add("gender", "F")
.add("phone", Json.createObjectBuilder()
.add("areacode", "505")
.add("number", "333-4444")))
.build();
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
11
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
12
JSON Pointer
• Specified in RFC 6901
• A string syntax for referencing a JSON value
• Example
– /0/phone/number
• refers to the phone number of first person in the contacts
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
13
JSON Pointer
Proposed API
JsonArray contacts = Json.createArrayBuilder().add(…).build();
// Create a JsonPointer
JsonPointer p = Json.createPointer("/0/phone/number");
// Get the value at the referenced location in the target
JsonValue v = p.getValue(contacts);
// Replace a value at the referenced location, returning a new array
// with the change
JsonArray result = p.replace(contacts, "123-4567");
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
14
JSON Pointer
Methods in JsonPointer
• getValue
– Get the value at the referenced location
• add
– Add/insert a value at the referenced location
• replace
– Replace a value at the referenced location
• remove
– Remove a value at the referenced location
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
15
JSON Pointer
Operations in JsonPointer
• An operation is applied to a JsonArray or JsonObject
• Operations do not modify the target JsonArray or JsonObject
• Operation add, replace, or remove returns a new JsonArray or JsonObject
containing the result
– Transforms the target into the result
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
16
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
17
JSON Patch
• Specified in RFC 6902
• Sequence of operations for modifying a JSON document
– Operations specified in a JSON array
• Suitable for use in http PATCH method
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
18
JSON Patch
Example
[
{"op":"replace", "path":"/1/phone", "value": {
"areacode": "111",
"number": "222-3333"}
}
{"from":"/1/phone", "path": "/2", "op":"copy"}
]
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
19
JSON Patch
Operations
• An patch operation is a JSON object
• Must have an “op” field with a value of
– “add”, “replace”, “remove”, “move”, “copy”, or “test”
• Must have a “path” field
– A JSON Pointer specifying the target location
• Other fields depending on “op”
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
20
JSON Patch
Proposed API
// Create the target and the patch
JsonArray target = Json.createArrayBuilder().add(…).build();
JsonArray patch = Json.createArrayBuilder()… .build();
// Create JsonPatch from patch
JsonPatch jsonpatch = Json.createPatch(patch);
// Apply the patch to the target and return the result
JsonArray result = jsonpatch.apply(target);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
21
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
22
JSON Editing/Transformation
• A JSON Patch transforms a JSON target to a JSON result
• Propose adding capability to edit a JsonArray or JsonObject
• Use builder pattern:
– Create builders with initial JsonArray or JsonObject
– Add to ObjectBuilder
• remove(name)
– Add to ArrayBuilder
• add(index, value), set(index, value), remove(index)
– Builder returns immutable JsonArray or JsonObject when done
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
23
JSON Editing
Proposed API
// Create the target
JsonArray target = Json.createArrayBuilder().add(…).build();
// Create a builder initialized with the target
JsonArrayBuilder builder = Json.createArrayBuilder(target);
// Creates a new object and insert it into the array
JsonObject John = Json.createObjectBuilder()… .build();
JsonArray result = builder.add(1, John)
.build();
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
24
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
25
JSON Query with Java SE 8
• A JsonObject is a Map, and a JsonArray is a List, so queries can be
implemented with Java’s stream operations, using Lambda expressions
• Example: Output names of contacts whose gender is F
JsonArray contacts;
contacts.getValuesAs(JsonObject.class).stream()
.filter(x->"F".equals(x.getString("gender")))
.map(x->x.getString("name"))
.forEach(System.out::println);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
26
JSON Query with Java SE 8
• Example: Collect query results in a List
JsonArray contacts;
List<String> names =
contacts.getValuesAs(JsonObject.class).stream()
.filter(x->"F".equals(x.getString("gender")))
.map(x->x.getString(“name”))
.collect(Collectors.toList());
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
27
JSON Query with Java SE 8
Problem
• Java Collectors return Lists or Maps.
• We need collectors that returns JsonArrays or JsonObjects
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
28
JSON Query with Java SE 8
Proposed API
• New helper class JsonCollectors that construct Collectors for JSON objects
or arrays
• toJsonArray:
– Accumulates values in a JsonArray
• toJsonObject:
– Accumulates values in a JsonObject
• groupBy
– Implements “group by” operations on the values
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
29
JSON Query with Java SE 8
Example
• Collect the query result in a JsonArray
JsonArray contacts;
JsonArray names =
contacts.getValuesAs(JsonObject.class).stream()
.filter(x->"F".equals(x.getString("gender")))
.map(x->x.getString("name"))
.collect(JsonCollectors.toJsonArray());
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
30
JSON Query with Java SE 8
Example
• Collect the query result in a JsonObject
JsonArray contacts;
JsonObject nameToPhones =
contacts.getValuesAs(JsonObject.class).stream()
.filter(x->"F".equals(x.getString("gender")))
.collect(JsonCollectors.toJsonObject(
x->x.getString("name"),
x->x.getJsonObject("phone"));
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
31
JSON Query with Java SE 8
Example
• Group the contacts by gender
JsonArray contacts;
JsonObject groups =
contacts.getValuesAs(JsonObject.class).stream()
.collect(JsonCollectors.groupBy(x->getString("gender")));
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
32
JSON Query with Java SE 8
Example: Putting it all together
• Change the phone areacodes from “415” to “650”
Static int index = -1;
JsonArray patch = contacts.getValuesAs(JsonObject.class).stream()
.peek(x->index++)
.filter(x->x.getObject("phone").getString("areacode").equals("415"))
.map(Json.createObjectBuilder()
.add("op", "replace")
.add("path", "/"+index+"/phone/areacode")
.add("value", “650”)
.build())
.collect(JsonCollectors.toJsonArray());
JsonArray result = Json.createPatch(patch).apply(contacts);
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
33
Program Agenda
1
JSON Object Model Overview
2
JSON Pointer
3
JSON Patch
4
JSON Editing/Transformation
5
JSON Query with Java SE 8
6
Big JSON data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
34
Support for big JSON data
• Big JSON data
– Cannot fit in memory
– Generated dynamically
– Potentially infinite
• We already have a stream model, suitable for processing big data
– Add API to make it more useful
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
35
Support for big JSON data
• Allow creation of partial JSON data in JsonParser
– Add getJsonObject() at the beginning of an object
– Add getJsonArray() at the beginning of an array
• Allow skipping values in JsonParser
• API for getting java.util.stream in JsonParser
– Lazy evaluation, pull model
– Low level: Stream of parse events
– High level: Stream of JsonObject and JsonArray elements
• Performance matters
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
36
Summary
• Support for JSON Pointer and JSON Patch
• Add editing/transformation to JsonObject and JsonArray
• Add help class/method for JSON query
• Add support for big JSON data
• WARNING: The API changes are just proposals, are not final
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
37
Q&A
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
38
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
39

similar documents