Jsonschema2Pojo

Description

jsonschema2pojo uses the structural rules present in a JSON Schema document to generate DTO style Java classes.

Parameters

Attribute Description Required
source The input JSON schema. Yes
targetDirectory The directory that generated .java source files will be saved to. Yes
annotationStyle The style of annotations to use in the generated Java types. Supported values:
  • JACKSON2 (apply annotations from the Jackson 2.x library)
  • JACKSON1 (apply annotations from the Jackson 1.x library)
  • GSON (apply annotations from the Gson library)
  • NONE (apply no annotations at all)
No (default JACKSON2)
classpath Additional classpath to use. Any path elements provided here will be added to the classpath when this task is invoked.

If a support for filesets/filelists/dirsets etc is required, this property can also be set as a nested element like:

<jsonschema2pojo ...
    <classpath>
        <fileset dir="custom-libs">
            <include name="**/*.jar" />
        </fileset>
    </classpath>
<jsonschema2pojo/>
No
classpathRef Additional classpath to use, given as a reference to a path defined elsewhere. Can be used in conjuction with the classpath option (the result is the union of all paths). No
customAnnotator

A fully qualified class name, referring to a custom annotator class that implements org.jsonschema2pojo.Annotator and will be used in addition to the one chosen by annotationStyle.

If you want to use the custom annotator alone, set annotationStyle to none.

No
customRuleFactory

A fully qualified class name, referring to a custom rule factory class that extends org.jsonschema2pojo.rules.RuleFactory.

By specifying a custom implementation you can customize most aspects of code generation by returning custom instances of rules.

No
generateBuilders Whether to generate builder-style methods of the form withXxx(value) (that return this), alongside the standard, void-return setters. No (default false)
includeHashcodeAndEquals Whether to use include hashCode and equals methods in generated Java types. No (default true)
includeConstructors Whether to generate constructors for generated Java types No (default false)
constructorsRequiredPropertiesOnly Whether generated constructors should have parameters for all properties, or only required ones. No (default false)
includeJsr303Annotations Whether to include JSR-303/349 annotations (for schema rules like minimum, maximum, etc) in generated Java types.

Schema rules and the annotation they produce:

  • maximum = @DecimalMax
  • minimum = @DecimalMin
  • minItems,maxItems = @Size
  • minLength,maxLength = @Size
  • pattern = @Pattern
  • required = @NotNull
Any Java fields which are an object or array of objects will be annotated with @Valid to support validation of an entire document tree.
No (default false)
includeToString Whether to use include a toString method in generated Java types. No (default true)
outputEncoding The character encoding that should be used when writing the generated Java source files. No (default UTF-8)
propertyWordDelimiters A string containing any characters that should act as word delimiters when choosing Java bean property names. No (default "- _" (hyphen, space, underscore))
removeOldOutput Whether to empty the target directory before generation occurs, to clear out all source files that have been generated previously.

Be warned, when activated this option will cause jsonschema2pojo to indiscriminately delete the entire contents of the target directory (all files and folders) before it begins generating sources.

No (default JSONSCHEMA)
skip Whether to skip type generation entirely (useful when set via a build property to allow quick disabling of this task using a command line property). No (default false)
sourceType The type of input documents that will be read

Supported values:

  • JSONSCHEMA (schema documents, containing formal rules that describe the structure of json data)
  • JSON (documents that represent an example of the kind of json data that the generated Java types will be mapped to)
No (default JSONSCHEMA)
targetPackage Package name used for generated Java classes (for types where a fully qualified name has not been supplied in the schema using the 'javaType' property). No
useCommonsLang3 Whether to use commons-lang 3.x imports instead of commons-lang 2.x imports when adding equals, hashCode and toString methods. No (default false)
useDoubleNumbers Whether to use the java type double (or Double) instead of float (or Float) when representing the JSON Schema type 'number'. No (default true)
useJodaDates Whether to use org.joda.time.DateTime instead of java.util.Date when adding date-time type fields to generated Java types. No (default false)
useJodaLocalDates Whether to use org.joda.time.LocalDate instead of String when adding string fields with format date to generated Java types. No (default false)
useJodaLocalTimes Whether to use org.joda.time.LocalTime instead of String when adding string fields with format time to generated Java types. No (default false)
useLongIntegers Whether to use the java type long (or Long) instead of int (or Integer) when representing the JSON Schema type 'integer'. No (default false)
usePrimitives Whether to use primitives (long, double, boolean) instead of wrapper types where possible when generating bean properties (has the side-effect of making those. No (default false)
initializeCollections Whether to initialize Set and List fields as empty collections, or leave them as null. Yes (default true)
classNamePrefix Whether to add a prefix to generated classes No (default "")
classNameSuffix Whether to add a Suffix to generated classes No (default "")

Examples

<taskdef name="jsonschema2pojo" classname="org.jsonschema2pojo.ant.Jsonschema2PojoTask">
    <classpath> <!-- classpath only required if jars have *NOT* been added to $ANT_HOME/lib -->
        <fileset dir="my-downloaded-libs">
            <include name="**/*.jar" />
        </fileset>
    </classpath>
</taskdef>

<target name="generate">
    <jsonschema2pojo source="address.json"
                     targetDirectory="build/generated-types"
                     targetPackage="com.example"/>
</target>
    

The above uses the taskdef task to make the jsonschema2pojo task available. The generate target invokes the jsonschema2pojo task to generate java classes based on an input schema called address.json.