Skip to main content

JSON-Schema Support

Juneau provides the JsonSchemaSerializer class for generating JSON-Schema documents that describe the output generated by the JsonSerializer class. This class shares the same properties as JsonSerializer.

For convenience the JsonSerializer.getSchemaSerializer() method has been added for creating instances of schema serializers from the regular serializer instance.

Sample Beans
public class Person {

// Bean properties
public String name;
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
}

The code for creating our POJO model and generating JSON-Schema is shown below:

// Get the one of the default schema serializers.
JsonSchemaSerializer serializer = JsonSchemaSerializer.DEFAULT_SIMPLE_READABLE;

// Get the JSON Schema for the POJO.
String jsonSchema = serializer.serialize(new Person());

// This also works.
jsonSchema = serializer.serialize(Person.class);
JSON Schema
{
type: 'object',
description: 'org.apache.juneau.sample.Person',
properties: {
name: {
type: 'string',
description: 'java.lang.String'
},
birthDate: {
type: 'string',
description: 'java.util.Calendar'
},
addresses: {
type: 'array',
description: 'java.util.LinkedList',
items: {
type: 'object',
description: 'org.apache.juneau.sample.Address',
properties: {
street: {
type: 'string',
description: 'java.lang.String'
},
city: {
type: 'string',
description: 'java.lang.String'
},
state: {
type: 'string',
description: 'java.lang.String'
},
zip: {
type: 'number',
description: 'int'
},
isCurrent: {
type: 'boolean',
description: 'boolean'
}
}
}
}
}
}