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!