X

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

Guest Author


Java API for JSON Processing (href="http://jcp.org/en/jsr/detail?id=353">JSR 353) href="http://jcp.org/en/jsr/results?id=5413">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 href="http://java.net/projects/javaee-spec/pages/Home#Java_EE_7_Schedule">Java
EE
7 schedule, the specification will be final on 4/15/2013.
The implementation is already integrated in href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">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 href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/4fcf4a61827b3aa703f962198e22e35e/totd205_json_samples.zip">downloaded
here, try it with href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b76.zip">GlassFish
4
b76 or href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">later.



Here are some more links for you to read further:
  • href="http://json-processing-spec.java.net/nonav/releases/1.0/pr-draft/javadocs/index.html">Public
    Review
    javadoc
  • href="http://json-processing-spec.java.net/nonav/releases/1.0/pfd-draft2/javadocs/index.html">Latest
    javadoc
  • Reference Implemenation: jsonp.java.net



Join the discussion

Comments ( 4 )
  • Jonathan Wednesday, February 20, 2013

    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.


  • Arun Gupta Thursday, February 21, 2013

    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


  • guest Tuesday, February 26, 2013

    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();

    }


  • Arun Gupta Wednesday, February 27, 2013

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


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