JsonGenerator and JsonObjectBuilder - Streaming and Object API in Java API for JSON Processing (TOTD #205)


Java API for JSON Processing (JSR 353) cleared Public Review unanimously and is on its way to to standardization. There is still Proposed Final Draft and the final vote to come. As per the Java EE 7 schedule, the specification will be final on 4/15/2013. The implementation is already integrated in GlassFish 4 builds.

The API provides an Object Model (like DOM for XML) and Streaming API (like StAX for XML) to parse and generate JSON structure. Here is a table that provide code fragments for generating some common JSON:

JSON Object Model API Streaming API
{ }
JsonObject jsonObject =
     new JsonObjectBuilder().build();

new JsonWriter(System.out)
     .writeObject(jsonObject);

JsonGeneratorFactory factory =
     Json.createGeneratorFactory();

JsonGenerator gen =
     factory.createGenerator(System.out);

gen.writeStartObject().writeEnd();
{
  "apple":"red",
  "banana":"yellow"
}
new JsonObjectBuilder()
  .add("apple", "red")
  .add("banana", "yellow")
.build();
gen.writeStartObject()
     .write("apple", "red")
     .write("banana", "yellow")
   .writeEnd();
[
  { "apple":"red" },
  { "banana":"yellow" }
]
JsonArray jsonArray = new JsonArrayBuilder()
  .add(new JsonObjectBuilder()
          .add("apple","red"))

  .add(new JsonObjectBuilder()
          .add("banana","yellow"))

  .build();
gen.writeStartArray()
     .writeStartObject()
       .write("apple", "red")
     .writeEnd()
     .writeStartObject()
       .write("banana", "yellow")
     .writeEnd()
.writeEnd();
{
  "title":"The Matrix",
  "year":1999,
  "cast":[
    "Keanu Reaves",
    "Laurence Fishburne",
    "Carrie-Anne Moss"
  ]
}
new new JsonObjectBuilder()
  .add("title", "The Matrix")
  .add("year", 1999)
  .add("cast", new JsonArrayBuilder()
  .add("Keanu Reaves")
  .add("Laurence Fishburne")
  .add("Carrie-Anne Moss"))
.build();
gen.writeStartObject()
     .write("title", "The Matrix")
     .write("year", 1999)
     .writeStartArray("cast")
       .write("Keanu Reaves")
       .write("Laurence Fishburne")
       .write("Carrie-Anne Moss")
     .writeEnd()
   .writeEnd();

The source code for this sample can be downloaded here, try it with GlassFish 4 b76 or later.

Here are some more links for you to read further:

Comments:

Is it possible that the last example for Object Model API is something missing?
In my oppinion it is not needed to start with an array, only "cast" is an array: "title", "year" and "cast" are not in an array but are in an object: the "[" and "]" are missing.
I think that "new JsonArrayBuilder().add(" at the beginning is not needed and you can start immediately with "new JsonObjectBuilder().add(". The JsonArrayBuilder for "cast" is necessary.
If you compare with Streaming API which I think is correct.

Posted by Jonathan on February 20, 2013 at 02:41 AM PST #

Jonathan,

Thanks for paying close attention to the blog. You are absolutely right, JsonArrayBuilder is indeed redundant and I updated the blog entry. I also updated the sample code and confirmed that the expected fragment is indeed generated.

Thanks,
Arun

Posted by Arun Gupta on February 20, 2013 at 04:56 PM PST #

When I test the code on a GlassFish 4.0 b77 (using the javaee-api 7.0-b77 dependency), the output of DOMGeneratorServlet stops after

'Creating an empty object (using DOM generator)...
{}
'

I think, correct me when I'm wrong, because of the writeObject method of JsonWriter closing the underlying writer.

code fragment from JsonWriter:
public void writeObject(JsonObject object) {
...
generator.writeEnd().close();
}

Posted by guest on February 26, 2013 at 05:52 AM PST #

This issue (http://java.net/jira/browse/JSONP-3) has already been fixed and will be integrated soon.

Posted by Arun Gupta on February 26, 2013 at 06:02 PM PST #

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