JSON-P

22,102 views

Published on

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.

Published in: Technology, News & Politics

JSON-P

  1. 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1
  2. 2. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.2Java API for JSONProcessingReza RahmanGlassFish/Java EE Evangelistreza.rahman@oracle.com
  3. 3. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3The following is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release,and timing of any features or functionality described for Oracle’s productsremains at the sole discretion of Oracle.
  4. 4. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4Java 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
  5. 5. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5Streaming 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_ARRAYJsonParser
  6. 6. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}
  7. 7. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}START_OBJECT
  8. 8. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}KEY_NAME
  9. 9. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}VALUE_STRING
  10. 10. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}VALUE_NUMBER
  11. 11. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}START_ARRAY
  12. 12. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12Streaming APIJsonParser{"firstName": "John", "lastName": "Smith", "age": 25,"phoneNumber": [{ "type": "home", "number": "212 555-1234" },{ "type": "fax", "number": "646 555-4567" }]}END_ARRAY
  13. 13. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13Streaming 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_OBJECTevent = parser.next(); // KEY_NAMEevent = parser.next(); // VALUE_STRINGString name = parser.getString(); // "John”
  14. 14. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14Streaming API JsonGenerator – Generates JSON in a streaming way to outputsources– Similar to StAX’s XMLStreamWriter Created using :– Json.createGenerator(…),Json.createGeneratorFactory().createGenerator(…) Optionally, configured with features– e.g. pretty printing Allows method chainingJsonGenerator
  15. 15. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15Streaming APIJsonGeneratorJsonGenerator 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"}]
  16. 16. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16Object 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 builderJsonBuilder// builds empty JSON objectJsonObject obj =Json.createObjectBuilder().build();
  17. 17. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17Object Model APIJsonBuilderJsonArray 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"}]
  18. 18. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18Object Model API Reads JsonObject and JsonArray from input source– I/O Reader, InputStream (+ encoding) Optionally, configured with features Uses pluggable JsonParserJsonReader// Reads a JSON objecttry(JsonReader reader = Json.createReader(io)) {JsonObject obj = reader.readObject();}
  19. 19. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19Object 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 JsonGeneratorJsonWriter// Writes a JSON objecttry(JsonWriter writer = Json.createWriter(io)) {writer.writeObject(obj);}
  20. 20. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20Where 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 andcomments 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
  21. 21. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.21Graphic Section Divider
  22. 22. Copyright © 2012, Oracle and/or its affiliates. All rights reserved.22

×