View
19.339
Download
1
Category
Tags:
Preview:
DESCRIPTION
This fast-faced, code-centric lightning talk covers the new JSON-P (aka Java API for JSON Processing) API. JSON is quickly becoming the de facto data interchange format on the web, especially with powerful JavaScript clients, Ajax, REST and HTML 5 WebSockets. JSON-P is intended to provide a standard, vendor-neutral, ubiquitous solution for parsing, generating and querying JSON. It includes both a DOM-like Object Model API and a StAX-like Streaming API.
Citation preview
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.2
Java API for JSON ProcessingReza RahmanGlassFish/Java EE Evangelistreza.rahman@oracle.com
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3
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 © 2012, Oracle and/or its affiliates. All rights reserved.4
Java API for JSON Processing
Standard API to parse, generate, transform, query JSON Object Model and Streaming API -- similar to DOM and StAX Binding JSON to Java objects forthcoming
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5
Streaming API
JsonParser – Parses JSON in a streaming way from input sources– Similar to StAX’s XMLStreamReader, a pull parser
Created using :– Json.createParser(…), Json.createParserFactory().createParser(…)
– Optionally, configured with features
Parser state events : – START_ARRAY, START_OBJECT, KEY_NAME, VALUE_STRING,
VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, VALUE_NULL, END_OBJECT, END_ARRAY
JsonParser
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
START_OBJECT
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
KEY_NAME
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
VALUE_STRING
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
VALUE_NUMBER
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
START_ARRAY
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12
Streaming APIJsonParser
{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
END_ARRAY
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13
Streaming APIJsonParser{
"firstName": "John", "lastName": "Smith", "age": 25,
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
Event event = parser.next(); // START_OBJECT
event = parser.next(); // KEY_NAME
event = parser.next(); // VALUE_STRING
String name = parser.getString(); // "John”
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14
Streaming API
JsonGenerator – Generates JSON in a streaming way to output sources
– Similar to StAX’s XMLStreamWriter
Created using :– Json.createGenerator(…),
Json.createGeneratorFactory().createGenerator(…)
Optionally, configured with features – e.g. pretty printing
Allows method chaining
JsonGenerator
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15
Streaming APIJsonGenerator
JsonGenerator ge=Json.createGenerator(…);
ge.writeStartArray()
.writeStartObject()
.write("type", "home”)
.write("number", "212 555-1234")
.writeEnd()
.writeStartObject()
.write("type", "fax”)
.write("number", "646 555-4567")
.writeEnd()
.writeEnd()
.close();
[
{
"type": "home”,
"number": "212 555-1234"
},
{
"type": "fax”,
"number": "646 555-4567"
}
]
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16
Object Model API
Builder to build JsonObject and JsonArray from scratch Allows method chaining Type-safe (cannot mix array and object building methods) Can also use existing JsonObject and JsonArray in a builder
JsonBuilder
// builds empty JSON object
JsonObject obj = Json.createObjectBuilder().build();
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17
Object Model APIJsonBuilder
JsonArray value =
Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("type", "home")
.add("number", "212 555-1234")
)
.add(Json.createObjectBuilder()
.add("type", "fax")
.add("number", "646 555-4567")
)
.build();
[
{
"type": "home”,
"number": "212 555-1234"
},
{
"type": "fax”,
"number": "646 555-4567"
}
]
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18
Object Model API
Reads JsonObject and JsonArray from input source– I/O Reader, InputStream (+ encoding)
Optionally, configured with features Uses pluggable JsonParser
JsonReader
// Reads a JSON object
try(JsonReader reader = Json.createReader(io)) {
JsonObject obj = reader.readObject();
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19
Object Model API
Writes JsonObject and JsonArray to output source– I/O Writer, OutputStream (+ encoding)
Optionally, configured with features. For e.g. pretty printing Uses pluggable JsonGenerator
JsonWriter
// Writes a JSON object
try(JsonWriter writer = Json.createWriter(io)) {
writer.writeObject(obj);
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20
Where to Find Out More Read the draft spec and API docs at
http://json-processing-spec.java.net Join users@json-processing-spec.java.net and send questions and
comments GlassFish 4.0
– http://glassfish.java.net/
– http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/
JSON-P– http://jsonp.java.net
Slide Deck– http://www.slideshare.net/reza_rahman
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.21
Graphic Section Divider
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.22
Recommended