JSON
Juneau supports converting arbitrary POJOs to and from JSON using ultra-efficient serializers and parsers. The JsonSerializer converts POJOs directly to strict RFC 8259 JSON (double quotes) without intermediate DOM objects. The JsonParser creates POJOs from strict JSON only. For JSON5 syntax (single quotes, comments, etc.), use Json5Serializer and Json5Parser.
The following example shows JSON for a typical bean:
Sample Beans
public class Person {
// Bean properties
public String name;
@MarshalledProp(calendarFormat=CalendarFormat.ISO_INSTANT) public Calendar birthDate;
public List addresses;
// Getters/setters omitted
}
public class Address {
// Bean properties
public String street, city;
public StateEnum state;
public int zip;
public boolean isCurrent;
// Getters/setters omitted
}
Sample Code
Person person = new Person()
.name("John Smith")
.birthDate("1946-08-12T00:00:00Z")
.addresses(
new Address()
.street("100 Main Street")
.city("Anywhereville")
.state(NY)
.zip(12345)
.isCurrent(true)
);
Normal JSON
{
"name": "John Smith",
"birthDate": "1946-08-12T00:00:00Z",
"addresses": [
{
"street": "100 Main Street",
"city": "Anywhereville",
"state": "NY",
"zip": 12345,
"isCurrent": true
}
]
}
JSON5
{
name: 'John Smith',
birthDate: '1946-08-12T00:00:00Z',
addresses: [
{
street: '100 Main Street',
city: 'Anywhereville',
state: 'NY',
zip: 12345,
isCurrent: true
}
]
}
In This Section
📄️ 2.2.25.1. JSON Methodology
The JSON data type produced depends on the Java object type being serialized.
📄️ 2.2.25.2. JSON Serializers
The JsonSerializer class serializes POJOs (beans, collections, maps and simple values) into JSON text.
📄️ 2.2.25.3. JSON 5
The Json5Serializer class can be used to serialized POJOs
📄️ 2.2.25.4. JSON Parsers
Parse JSON text into Java beans, collections and maps with JsonParser.
📄️ 2.2.25.5. @Json Annotation
The @Json annotation is used to override the behavior of JsonSerializer on individual bean classes or properties.
📄️ 2.2.25.6. JSON-Schema Support
Juneau provides the JsonSchemaSerializer class for