Download presentation
Presentation is loading. Please wait.
2
JSR 353: Java API for JSON Processing
John Clingan Java EE and GlassFish Product Manager
3
Agenda JSON Overview JAX-RS and JSON JSR 353 JSON API
4
JSON Overview Lightweight Data Exchange Format
Easy for humans/machines to read and write Minimal. Compact, textual, and subset of JavaScript Example: {“name” : “John”, “age”:20, “phone”:[“ ”, “ ”]} Used heavily in RESTful web services, configuration, databases, browserserver communication
5
JSON Overview Used by popular websites in their RESTful Web Services
Facebook, Twitter, Amazon Twitter Streaming API discontinued XML
6
Google Custom Search API
ee&alt=json { "kind": "customsearch#search", "url": { "type": "application/json", "template": " }, "queries": { "request": [ "title": "Google Custom Search - javaee", "totalResults": "22", "searchTerms": "javaee", "count": 10, }
7
JAX-RS XML Usage JAX-RS applications handle XML using JAXP API
@Produces(“application/xml”) public Source getBook(String id) { return new StreamSource(…); }
8
JAX-RS Data Binding JAX-RS applications handle XML using JAXB API
@Produces(“application/xml”, “application/json”) public Book getBook(String id) { return new Book(…); } JAX-RS Book XML JAXB JSON ?
9
JAX-RS JSON implementations for Java json-simple mjson Json-marshaller
Jackson SOJO org.json json fastjson json-taglib Argo Stringtree jjson google-gson json-io Flexjson Json-smart Jettison
10
JAX-RS JSON Solutions and Limitations
A custom MessageBodyWriter that converts to JSON JSONObject (Ex: json.org’s API) JSON POJO/JAXB XML JSON (Ex: using jettison) POJO/JAXB JSON (Ex: jackson, EclipseLink, etc) No standard API Some solutions have technical limitations Applications/frameworks need to bundle implementation
11
Standard API Concepts JSR 353: Parsing/processing JSON JSR Forthcoming
Similar to JAXP Expert group Oracle, Red Hat, Twitter Individuals (GreenTea JUG/Wen Shao, Werner Keil, Christian Grobmeier) Community JSR Forthcoming Data binding - JSON text Java Objects Similar to JAXB
12
JSR Transparency Json-processing-spec.java.net open source project Mailing lists Archived Issue Tracker
13
Java API for JSON Processing
Overview – JSR 353 API to parse and generate JSON Streaming API Low-level, efficient way to parse/generate JSON Provides pluggability for parsers/generators Object Model Simple, easy-to-use high-level API Implemented on top of Streaming API Binding JSON to Java objects forthcoming Streaming API is much more efficient, no intermediate objects created Object model API is built on top of Streaming API Forward-only parser, each parser can be configured with multiple features
14
JSON Processing API Architecture
… App 1 App 2 App N Object Model API Streaming API SPI Provider
15
Java API for JSON Processing
Streaming API Parses JSON in a streaming way from input sources Similar to StaX’s XMLStreamReader, a pull parser Created using Json.createParser(…) Json.createParserFactory().createParser(…) Parser state events START_ARRAY, END_ARRAY, START_OBJECT, END_OBJECT, …
16
Java API for JSON Processing
Streaming Parser { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] }
17
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } START_OBJECT
18
Java API for JSON Processing
Streaming API KEY_NAME { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] }
19
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } VALUE_STRING
20
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } VALUE_NUMBER
21
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } START_ARRAY
22
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } END_ARRAY
23
Java API for JSON Processing
Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": " " }, { "type": "fax", "number": " " } ] } JsonParser parser = Json.createParser(request.getReader()); Event event = parser.next(); // START_OBJECT event = parser.next(); // KEY_NAME event = parser.next(); // VALUE_STRING String name = parser.getString(); // "John”
24
Java API for JSON Processing
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. for pretty printing
25
Java API for JSON Processing
Streaming API JsonGenerator jg = Json.createGenerator(…); jg. .writeStartArray("phoneNumber") .writeStartObject() write("type", "home") write("number", " ") writeEnd() writeStartArray() write("type", ”work") write("number", " ") writeEnd() .writeEnd(); jg.close(); "phoneNumber": [ { "type": "home", "number": ” ” }, { "type": ”work", "number": ” ” } ] Typesafe mapping
26
Java API for JSON Processing 1.0
Object Model API JsonObject/JsonArray – JSON object and array structures JsonString and JsonNumber for string and number values JsonObjectBuilder – Builds JsonObject JsonArrayBuilder – Builds JsonArray JsonReader – Reads JsonObject and JsonArray from input source JsonWriter – Writes JsonObject and JsonArray to output source
27
Java API for JSON Processing
Object API Reads JsonObject and JsonArray from input source i/o Reader, InputStream (+ encoding) Optionally, configured with features Uses pluggable JsonParser // Reads a JSON object try(JsonReader reader = Json.createReader(io)) { JsonObject obj = reader.readObject(); } Typesafe mapping
28
Java API for JSON Processing
Object 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 Typesafe mapping // Writes a JSON object try(JsonWriter writer = Json.createWriter(io)) { writer.writeObject(obj); }
29
Java API for JSON Processing
JsonObject Holds name/value pairs and immutable Name/value pairs can be accessed as Map<String, JsonValue> JsonObject obj = reader.readObject(); Map<String, JsonValue> map = obj.getValues(); String str = obj.getStringValue(“foo”); Set<String> names = obj.getNames();
30
Java API for JSON Processing
Configuration Support configuration of parser/generator features, extensibility Pretty Printing, Single-Quoted strings Can be used in streaming & object-model API Pass in a map Json.createBuilderFactory(Map<String, ?> config) Json.createGeneratorFactory(Map<String, ?> config) Json.createParserFactory(Map<String, ?> config) Json.createReaderFactory(Map<String, ?> config) Json.createWriterFactory(Map<String, ?> config) Typesafe mapping
31
TOGETHER WE CAN MOVE JAVA EE
Amazing JSR Adopters TOGETHER WE CAN MOVE JAVA EE FORWARD Your JUG here Overview of program Significant contribution, incl SDK – example (verbal only) Tic-Tac-Toe from Belgian JUG and note we will see them later on These are a sampling of past participating JUGs Reiterate community value Note their JUG can be on the list Transition to JSR videoof what this looks like/value of participation
32
Summary JSON Overview JAX-RS and JSON JSR 353 JSON API
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.