javax.json
Class JsonBuilder

java.lang.Object
  extended by javax.json.JsonBuilder

public class JsonBuilder
extends Object

Builds a JSON object or array from scratch. It uses builder pattern to build these object models and the builder methods can be chained.

For example, for the following JSON

 {
     "firstName": "John", "lastName": "Smith", "age": 25,
     "address" : {
         "streetAddress", "21 2nd Street",
         "city", "New York",
         "state", "NY",
         "postalCode", "10021"
     },
     "phoneNumber": [
         { "type": "home", "number": "212 555-1234" },
         { "type": "fax", "number": "646 555-4567" }
     ]
 }
 
a JsonObject instance can be built using:

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

Author:
Jitendra Kotamraju

Nested Class Summary
static interface JsonBuilder.JsonBuildable<T extends JsonStructure>
          Build task that gives the result of the build process
 
Constructor Summary
JsonBuilder()
           
 
Method Summary
 JsonArrayBuilder<JsonBuilder.JsonBuildable<JsonArray>> beginArray()
          Start building a JSON array
 JsonObjectBuilder<JsonBuilder.JsonBuildable<JsonObject>> beginObject()
          Start building a JSON object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonBuilder

public JsonBuilder()
Method Detail

beginObject

public JsonObjectBuilder<JsonBuilder.JsonBuildable<JsonObject>> beginObject()
Start building a JSON object

Returns:
an object builder

beginArray

public JsonArrayBuilder<JsonBuilder.JsonBuildable<JsonArray>> beginArray()
Start building a JSON array

Returns:
an array builder



Copyright © 2012 Oracle and/or its affiliates. All rights reserved.