1 / 33

John Clingan Java EE and GlassFish Product Manager john.clingan@oracle

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

rumor
Download Presentation

John Clingan Java EE and GlassFish Product Manager john.clingan@oracle

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSR 353: Java API for JSON Processing John Clingan Java EE and GlassFish Product Manager john.clingan@oracle.com

  2. JSON Overview • JAX-RS and JSON • JSR 353 • JSON API Agenda

  3. 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, browserserver communication

  4. JSON Overview • Used by popular websites in their RESTful Web Services • Facebook, Twitter, Amazon • Twitter Streaming API discontinued XML

  5. 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, ...}

  6. JAX-RS XML Usage • JAX-RS applications handle XML using JAXP API @Produces(“application/xml”) public Source getBook(String id) { return new StreamSource(…); }

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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. JSON Processing API Architecture … App 1 App 2 App N Object Model API Streaming API SPI Provider

  14. 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, …

  15. 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" } • ] • }

  16. 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" } ] }

  17. 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" } ] }

  18. 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" } ] }

  19. 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" } ] }

  20. 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

  21. 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

  22. 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”

  23. 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

  24. 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” } ]

  25. 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

  26. 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(); • }

  27. 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); • }

  28. 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();

  29. 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)

  30. Amazing JSR Adopters TOGETHERWE CAN MOVE JAVA EE FORWARD Your JUG here

  31. JSON Overview • JAX-RS and JSON • JSR 353 • JSON API Summary

More Related