public class JsonObjectBuilder extends Object
JsonObject
from scratch. It uses builder pattern to build
the object model and the builder methods can be chained while building the
JSON Object.
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 JsonObjectBuilder()
.add("firstName", "John")
.add("lastName", "Smith")
.add("age", 25)
.add("address", new JsonObjectBuilder()
.add("streetAddress", "21 2nd Street")
.add("city", "New York")
.add("state", "NY")
.add("postalCode", "10021"))
.add("phoneNumber", new JsonArrayBuilder()
.add(new JsonObjectBuilder()
.add("type", "home")
.add("number", "212 555-1234"))
.add(new JsonObjectBuilder()
.add("type", "fax")
.add("number", "646 555-4567")))
.build();
JsonArrayBuilder
Constructor and Description |
---|
JsonObjectBuilder()
Constructs a
JsonObjectBuilder that initializes an empty JSON
object that is being built. |
Modifier and Type | Method and Description |
---|---|
JsonObjectBuilder |
add(String name,
BigDecimal value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
BigInteger value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
boolean value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
double value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
int value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
JsonArrayBuilder builder)
Associates the JSON array from the specified builder with the
specified name in the JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
JsonObjectBuilder builder)
Associates the JsonObject from the specified builder with the
specified name in the JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
JsonValue value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
long value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
add(String name,
String value)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObjectBuilder |
addNull(String name)
Associates the specified value with the specified name in the
JSON object that is being built.
|
JsonObject |
build()
Returns the JSON object that is being built.
|
public JsonObjectBuilder()
JsonObjectBuilder
that initializes an empty JSON
object that is being built.public JsonObjectBuilder add(String name, JsonValue value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified namepublic JsonObjectBuilder add(String name, String value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified namepublic JsonObjectBuilder add(String name, BigInteger value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified nameJsonNumber
public JsonObjectBuilder add(String name, BigDecimal value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified nameJsonNumber
public JsonObjectBuilder add(String name, int value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified nameJsonNumber
public JsonObjectBuilder add(String name, long value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified nameJsonNumber
public JsonObjectBuilder add(String name, double value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified nameNumberFormatException
- if value is Not-a-Number(NaN) or infinityJsonNumber
public JsonObjectBuilder add(String name, boolean value)
name
- name with which the specified value is to be associatedvalue
- value to be associated with the specified namepublic JsonObjectBuilder addNull(String name)
name
- name with which the specified value is to be associatedpublic JsonObjectBuilder add(String name, JsonObjectBuilder builder)
name
- name with which the specified value is to be associatedpublic JsonObjectBuilder add(String name, JsonArrayBuilder builder)
name
- name with which the specified value is to be associatedpublic JsonObject build()
Copyright © 2013. All Rights Reserved.