X

JSON-P: Java API for JSON Processing (TOTD #178)

Guest Author


JSR 353 is Java
API for JSON Processing (JSON-P) and will define an API to process
(e.g. parse, generate, transform, and query) JSON. This JSR will be
delivered as part of Java EE 7. The API will allow to produce and
consume JSON in a streaming fashion (StAX equivalent in XML world)
and build a Java object model for JSON (DOM equivalent in XML
world). Note, binding JSON to Java objects and vice versa is not
part of the scope of this JSR.



json-processing-spec.java.net
is where all the specification work is happening and href="http://jsonp.java.net">jsonp.java.net is the project for
the  Reference Implementation. Its still early days but this style="font-weight: bold;">Tip Of The style="font-weight: bold;">Day (TOTD) will explain how to
get started. The workspace can be checked out as


git clone git://java.net/jsonp~git
Cloning into jsonp~git...
remote: Counting objects: 313, done.
remote: Compressing objects: 100% (218/218), done.
remote: Total 313 (delta 110), reused 0 (delta 0)
Receiving objects: 100% (313/313), 64.13 KiB, done.
Resolving deltas: 100% (110/110), done.



Building the workspace require JAVA_HOME to be set (/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
or /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home).
Once set, the workspace can be built as
mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] RI for JSON-P JSR
[INFO] Java API for Processing JSON (JSON-P)
[INFO] jsonp-tests
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building RI for JSON-P JSR 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------

. . .

Tests run: 22, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ jsonp-tests ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /Users/arungup/code/workspaces/jsonp~git/tests/target/jsonp-tests-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ jsonp-tests ---
[INFO] Installing /Users/arungup/code/workspaces/jsonp~git/tests/target/jsonp-tests-1.0-SNAPSHOT.jar to /Users/arungup/.m2/repository/org/glassfish/jsonp-tests/1.0-SNAPSHOT/jsonp-tests-1.0-SNAPSHOT.jar
[INFO] Installing /Users/arungup/code/workspaces/jsonp~git/tests/pom.xml to /Users/arungup/.m2/repository/org/glassfish/jsonp-tests/1.0-SNAPSHOT/jsonp-tests-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] RI for JSON-P JSR ................................. SUCCESS [0.528s]
[INFO] Java API for Processing JSON (JSON-P) ............. SUCCESS [12.214s]
[INFO] jsonp-tests ....................................... SUCCESS [1.695s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------



The API .jar file is in api/target/jsonp-ri-1.0-SNAPSHOT.jar
and javadocs are in api/target/jsonp-ri-1.0-SNAPSHOT-javadoc.jar.
There are several tests in the tests directory that
shows the API usage. The RI JARs are not pushed to a public maven
repo yet but are installed in the local repo with the above command.
These can be included in your "pom.xml" with the following
coordinates:


<dependency>
<groupId>javax.json</groupId>
<artifactId>jsonp-ri</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>



The key APIs are
  • DOM-based APIs (javax.json package)

    • JsonBuilder - Builds a JSON object or JSON array
    • JsonReader - Reads a JSON object or array from the stream
    • JsonWriter - Writes a JSON object or array to the stream
  • Streaming APIs (javax.json.stream package)

    • JsonGenerator - Streaming JSON generator

    • JsonParser - Allows forward, read-only access to JSON




Here is an sample usage of JsonBuilder:

JsonObject value = new JsonBuilder()

   .beginObject()

     .add("firstName", "John")

     .add("lastName", "Smith")

     .add("age", 25)

        .beginObject("address")


          
.add("streetAddress", "21 2nd Street")

          
.add("city", "New York")

          
.add("state", "NY")

          
.add("postalCode", "10021")

        .endObject()

       
.beginArray("phoneNumber")

          
.beginObject()

             
.add("type", "home")

             
.add("number", "212 555-1234")

          
.endObject()

          
.beginObject()

             
.add("type", "home")

             
.add("number", "646 555-4567")

          
.endObject()

        .endArray()

   .endObject()

.build();


Here is a sample usage of JsonReader:


String json = "...";
JsonReader reader = new JsonReader(new StringReader(json));
JsonValue value = reader.readObject();
reader.close();



A sample usage of JsonWriter:

JsonWriter jsonWriter = new JsonWriter(new FileWriter(...));
JsonObject jsonObject = new JsonBuilder()
.beginObject()
. . .
.endObject()
.build()
;
jsonWriter.writeObject(jsonObject);
jsonWriter.close();

Here is a sample usage of JsonGenerator:


JsonGenerator generator = new JsonGenerator(new FileWriter(...));
generator
.beginObject()
. . .
.beginArray()
. . .
.endArray()
.endObject()
.build();
generator.close();



And finally a sample usage of JsonParser:
String json = "...";
JsonParser parser = new JsonParser(new StringReader(json));
Iterator<Event> it = reader.iterator();
Event event = it.next();

The event  can be of the following types:
  • START_OBJECT
  • END_OBJECT
  • START_ARRAY
  • END_ARRAY
  • KEY_NAME
  • VALUE_STRING
  • VALUE_NUMBER

  • VALUE_FALSE
  • VALUE_TRUE
  • VALUE_NULL


Here are some references to track the progress and provide feedback:
  • Join the specification project at href="http://json-processing-spec.java.net">json-processing-spec.java.net
  • The Reference Implementation is built at href="http://jsonp.java.net">jsonp.java.netstyle="text-decoration: underline;">
  • Mailing Lists
    • users@json-processing-spec.java.net
    • jsr353-experts@json-processing.java.net
  • Issue Tracker: href="http://java.net/jira/browse/JSON_PROCESSING_SPEC">java.net/jira/browse/JSON_PROCESSING_SPEC


This JSR will be delivered as part of Java EE 7. Here are some other
early work that has been explained:
  • href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_early_draft">JPA
    2.1 Early Draft Explained
  • href="https://blogs.oracle.com/arungupta/entry/jax_rs_2_0_early">JAX-RS
    2.0 Early Draft Explained
  • What's new in JSF 2.2
    ?





Have fun!

Join the discussion

Comments ( 4 )
  • murat Tuesday, May 15, 2012

    The name might be confusing... jsonp is also know for 'json with padding' and is widely used in mobile apps since usually the html is located on the device (localhost) and jsonp is the only way out from the sandbox.


  • icecube media Thursday, May 17, 2012

    Thanks for sharing this information.Its very helpful for the peoples to learn the java.


  • guest Thursday, May 17, 2012

    Hmm, where is the difference to JAXB in combination with JSON de-/serializer?

    Looks to me like reinventing the wheel?

    rgds,

    Max


  • guest Wednesday, May 1, 2013

    Rather than comparing it to JAXB it's more related to Jackson/Jettison, that is a low-level JSON generator/reader library.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.