340 likes | 541 Views
JSR 353: Java API for JSON Processing. John Clingan Java EE and GlassFish Product Manager john.clingan@oracle.com. JSON Overview JAX-RS and JSON JSR 353 JSON API. Agenda. JSON Overview. Lightweight Data Exchange Format. Easy for humans/machines to read and write
E N D
JSR 353: Java API for JSON Processing John Clingan Java EE and GlassFish Product Manager john.clingan@oracle.com
JSON Overview • JAX-RS and JSON • JSR 353 • JSON API Agenda
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”:[“2761234”, “1234567”]} • Used heavily in RESTful web services, configuration, databases, browserserver communication
JSON Overview • Used by popular websites in their RESTful Web Services • Facebook, Twitter, Amazon • Twitter Streaming API discontinued XML
Google Custom Search API www.googleapis.com/customsearch/v1?key=[your-key]&cx=yourcx&q=java ee&alt=json { "kind": "customsearch#search", "url": { "type": "application/json", "template": "https://www.googleapis.com/customsearch/v1?q=..."} }, "queries": { "request": [ { "title": "Google Custom Search - javaee", "totalResults": "22", "searchTerms": "javaee", "count": 10, ...}
JAX-RS XML Usage • JAX-RS applications handle XML using JAXP API @Produces(“application/xml”) public Source getBook(String id) { return new StreamSource(…); }
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(…); } XML JAX-RS JAXB Book ? JSON
JAX-RS JSON implementations for Java json-simple Json-marshaller mjson Jackson SOJO org.json json fastjson json-taglib Argo Stringtree jjson google-gson Flexjson json-io Json-smart Jettison
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
Standard API Concepts • JSR 353: Parsing/processing JSON • Similar to JAXP • Expert group • Oracle, Red Hat, Twitter • Individuals (GreenTea JUG/WenShao, Werner Keil, Christian Grobmeier) • Community • JSR Forthcoming • Data binding - JSON text Java Objects • Similar to JAXB
JSR 353 - Transparency • Json-processing-spec.java.net open source project • Mailing lists • users@json-processing-spec.java.net • jsr353-experts@json-processing-spec.java.net • Archived • Issue Tracker • http://java.net/jira/browse/JSON_PROCESSING_SPEC
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
JSON Processing API Architecture … App 1 App 2 App N Object Model API Streaming API SPI Provider
Java API for JSON Processing Streaming API • Parses JSON in a streaming way from input sources • Similar to StaX’sXMLStreamReader, a pull parser • Created using • Json.createParser(…) • Json.createParserFactory().createParser(…) • Parser state events • START_ARRAY, END_ARRAY, START_OBJECT, END_OBJECT, …
Java API for JSON Processing Streaming Parser • { • "firstName": "John", "lastName": "Smith", "age": 25, • "phoneNumber": [ • { "type": "home", "number": "212 555-1234" }, • { "type": "fax", "number": "646 555-4567" } • ] • }
Java API for JSON Processing Streaming API START_OBJECT { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
Java API for JSON Processing Streaming API KEY_NAME { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
Java API for JSON Processing Streaming API VALUE_STRING { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
Java API for JSON Processing Streaming API VALUE_NUMBER { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
Java API for JSON Processing Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] } START_ARRAY
Java API for JSON Processing Streaming API { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] } END_ARRAY
Java API for JSON Processing Streaming API {"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" } ]} JsonParser parser = Json.createParser(request.getReader());Event event = parser.next(); // START_OBJECTevent = parser.next(); // KEY_NAMEevent = parser.next(); // VALUE_STRINGString name = parser.getString();// "John”
Java API for JSON Processing Streaming API • JsonGenerator • Generates JSON in a streaming way to output sources • Similar to StaX’sXMLStreamWriter • Created using • Json.createGenerator(…) • Json.createGeneratorFactory().createGenerator(…) • Optionally, configured with features • E.g. for pretty printing
Java API for JSON Processing Streaming API JsonGeneratorjg= Json.createGenerator(…);jg..writeStartArray("phoneNumber").writeStartObject() .write("type", "home") .write("number", "408-123-4567") .writeEnd() .writeStartArray() .write("type", ”work") .write("number", "408-987-6543") .writeEnd() .writeEnd();jg.close(); "phoneNumber": [ { "type": "home", "number": ”408-123-4567” }, { "type": ”work", "number": ”408-987-6543” } ]
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
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)) { • JsonObjectobj = reader.readObject(); • }
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 • // Writes a JSON object • try(JsonWriter writer = Json.createWriter(io)) { • writer.writeObject(obj); • }
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();
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)
Amazing JSR Adopters TOGETHERWE CAN MOVE JAVA EE FORWARD Your JUG here
JSON Overview • JAX-RS and JSON • JSR 353 • JSON API Summary