1 Copyright 2012 Oracle andor its affiliates All

  • Slides: 33
Download presentation
1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JSR 353: Java API for JSON Processing John Clingan Java EE and Glass. Fish

JSR 353: Java API for JSON Processing John Clingan Java EE and Glass. Fish Product Manager john. clingan@oracle. com 2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Agenda § JSON Overview § JAX-RS and JSON § JSR 353 § JSON API

Agenda § JSON Overview § JAX-RS and JSON § JSR 353 § JSON API 3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JSON Overview Lightweight Data Exchange Format § Easy for humans/machines to read and write

JSON Overview Lightweight Data Exchange Format § Easy for humans/machines to read and write § Minimal. Compact, textual, and subset of Java. Script § Example: {“name” : “John”, “age”: 20, “phone”: [“ 2761234”, “ 1234567”]} § Used heavily in RESTful web services, configuration, databases, browser server communication 4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JSON Overview § Used by popular websites in their RESTful Web Services – Facebook,

JSON Overview § Used by popular websites in their RESTful Web Services – Facebook, Twitter, Amazon – Twitter Streaming API discontinued XML 5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Google Custom Search API www. googleapis. com/customsearch/v 1? key=[your-key]&cx=yourcx&q=java ee&alt=json { "kind": "customsearch#search", "url":

Google Custom Search API www. googleapis. com/customsearch/v 1? key=[your-key]&cx=yourcx&q=java ee&alt=json { "kind": "customsearch#search", "url": { "type": "application/json", "template": "https: //www. googleapis. com/customsearch/v 1? q=. . . "} }, "queries": { "request": [ { "title": "Google Custom Search - javaee", "total. Results": "22", "search. Terms": "javaee", "count": 10, . . . } 6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JAX-RS XML Usage § JAX-RS applications handle XML using JAXP API @Produces(“application/xml”) public Source

JAX-RS XML Usage § JAX-RS applications handle XML using JAXP API @Produces(“application/xml”) public Source get. Book(String id) { return new Stream. Source(…); } 7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JAX-RS Data Binding § JAX-RS applications handle XML using JAXB API @Produces(“application/xml”, “application/json”) public

JAX-RS Data Binding § JAX-RS applications handle XML using JAXB API @Produces(“application/xml”, “application/json”) public Book get. Book(String id) { return new Book(…); } 8 XML JAX JSON ? Copyright © 2012, Oracle and/or its affiliates. All rights reserved. B JAX-RS Book

JAX-RS JSON implementations for Java Jackson json-simple mjson SOJO org. json Json-marshaller fastjson Argo

JAX-RS JSON implementations for Java Jackson json-simple mjson SOJO org. json Json-marshaller fastjson Argo Stringtree Flexjson-io Jettison 9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. jjson Json-smart json-taglib google-gson

JAX-RS JSON Solutions and Limitations § A custom Message. Body. Writer that converts to

JAX-RS JSON Solutions and Limitations § A custom Message. Body. Writer that converts to JSON – JSONObject (Ex: json. org’s API) JSON – POJO/JAXB XML JSON (Ex: using jettison) – POJO/JAXB JSON (Ex: jackson, Eclipse. Link, etc) § No standard API § Some solutions have technical limitations § Applications/frameworks need to bundle implementation 10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Standard API Concepts § JSR 353: Parsing/processing JSON – Similar to JAXP – Expert

Standard API Concepts § JSR 353: Parsing/processing JSON – Similar to JAXP – Expert group § Oracle, Red Hat, Twitter § Individuals (Green. Tea JUG/Wen Shao, Werner Keil, Christian Grobmeier) § Community § JSR Forthcoming – Data binding - JSON text Java Objects – Similar to JAXB 11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JSR 353 - Transparency § Json-processing-spec. java. net open source project § Mailing lists

JSR 353 - Transparency § Json-processing-spec. java. net open source project § Mailing lists – users@json-processing-spec. java. net – jsr 353 -experts@json-processing-spec. java. net – Archived § Issue Tracker – http: //java. net/jira/browse/JSON_PROCESSING_SPEC 12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Overview – JSR 353 § API to parse and

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 13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

JSON Processing API Architecture App 1 App 2 … Object Model API Streaming API

JSON Processing API Architecture App 1 App 2 … Object Model API Streaming API SPI Provider 14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. App N

Java API for JSON Processing Streaming API § Parses JSON in a streaming way

Java API for JSON Processing Streaming API § Parses JSON in a streaming way from input sources – Similar to Sta. X’s XMLStream. Reader, a pull parser § Created using – Json. create. Parser(…) – Json. create. Parser. Factory(). create. Parser(…) § Parser state events – START_ARRAY, END_ARRAY, START_OBJECT, END_OBJECT, … 15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming Parser { "first. Name": "John", "last. Name": "Smith",

Java API for JSON Processing Streaming Parser { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API { START_OBJECT "first. Name": "John", "last. Name":

Java API for JSON Processing Streaming API { START_OBJECT "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API KEY_NAME { "first. Name": "John", "last. Name":

Java API for JSON Processing Streaming API KEY_NAME { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API { VALUE_STRING "first. Name": "John", "last. Name":

Java API for JSON Processing Streaming API { VALUE_STRING "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API VALUE_NUMBER { "first. Name": "John", "last. Name":

Java API for JSON Processing Streaming API VALUE_NUMBER { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith",

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ START_ARRAY { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } 21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith",

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] END_ARRAY } 22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith",

Java API for JSON Processing Streaming API { "first. Name": "John", "last. Name": "Smith", "age": 25, "phone. Number": [ { "type": "home", "number": "212 555 -1234" }, { "type": "fax", "number": "646 555 -4567" } ] } Json. Parser parser = Json. create. Parser(request. get. Reader()); Event event = parser. next(); // START_OBJECT event = parser. next(); // KEY_NAME event = parser. next(); // VALUE_STRING String name = parser. get. String(); // "John” 23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API § Json. Generator – Generates JSON in

Java API for JSON Processing Streaming API § Json. Generator – Generates JSON in a streaming way to output sources § Similar to Sta. X’s XMLStream. Writer – Created using § Json. create. Generator(…) § Json. create. Generator. Factory(). create. Generator(…) – Optionally, configured with features § E. g. for pretty printing 24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Streaming API Json. Generator jg = Json. create. Generator(…);

Java API for JSON Processing Streaming API Json. Generator jg = Json. create. Generator(…); "phone. Number": [ { "type": "home", "number": ” 408 -123 -4567” }, { "type": ”work", "number": ” 408 -987 -6543” } ] 25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. jg. . write. Start. Array("phone. Number"). write. Start. Object(). write("type", "home"). write("number", "408 -123 -4567"). write. End(). write. Start. Array(). write("type", ”work"). write("number", "408 -987 -6543"). write. End(); jg. close();

Java API for JSON Processing 1. 0 Object Model API § Json. Object/Json. Array

Java API for JSON Processing 1. 0 Object Model API § Json. Object/Json. Array – JSON object and array structures – Json. String and Json. Number for string and number values § Json. Object. Builder – Builds Json. Object § Json. Array. Builder – Builds Json. Array § Json. Reader – Reads Json. Object and Json. Array from input source § Json. Writer – Writes Json. Object and Json. Array to output source 26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Object API § Reads Json. Object and Json. Array

Java API for JSON Processing Object API § Reads Json. Object and Json. Array from input source – i/o Reader, Input. Stream (+ encoding) § Optionally, configured with features § Uses pluggable Json. Parser // Reads a JSON object try(Json. Reader reader = Json. create. Reader(io)) { Json. Object obj = reader. read. Object(); } 27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Object API § Writes Json. Object and Json. Array

Java API for JSON Processing Object API § Writes Json. Object and Json. Array to output source – i/o Writer, Output. Stream (+ encoding) § Optionally, configured with features. For e. g. pretty printing § Uses pluggable Json. Generator // Writes a JSON object try(Json. Writer writer = Json. create. Writer(io)) { writer. write. Object(obj); } 28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Json. Object § Holds name/value pairs and immutable §

Java API for JSON Processing Json. Object § Holds name/value pairs and immutable § Name/value pairs can be accessed as Map<String, Json. Value> Json. Object obj = reader. read. Object(); Map<String, Json. Value> map = obj. get. Values(); String str = obj. get. String. Value(“foo”); Set<String> names = obj. get. Names(); 29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Java API for JSON Processing Configuration § Support configuration of parser/generator features, extensibility –

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. create. Builder. Factory(Map<String, ? > config) – Json. create. Generator. Factory(Map<String, ? > config) – Json. create. Parser. Factory(Map<String, ? > config) – Json. create. Reader. Factory(Map<String, ? > config) – Json. create. Writer. Factory(Map<String, ? > config) 30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Amazing JSR Adopters TOGETHER WE CAN MOVE JAVA EE FORWARD JUG r u Yo

Amazing JSR Adopters TOGETHER WE CAN MOVE JAVA EE FORWARD JUG r u Yo e her 31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

Summary § JSON Overview § JAX-RS and JSON § JSR 353 § JSON API

Summary § JSON Overview § JAX-RS and JSON § JSR 353 § JSON API 32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved.