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


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 jsonp.java.net is the project for the  Reference Implementation. Its still early days but this Tip Of The 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:
This JSR will be delivered as part of Java EE 7. Here are some other early work that has been explained:

Have fun!
Comments:

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.

Posted by murat on May 15, 2012 at 05:53 AM PDT #

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

Posted by icecube media on May 16, 2012 at 11:26 PM PDT #

Hmm, where is the difference to JAXB in combination with JSON de-/serializer?
Looks to me like reinventing the wheel?

rgds,
Max

Posted by guest on May 17, 2012 at 04:54 AM PDT #

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

Posted by guest on May 01, 2013 at 01:37 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today