Extended jsonschema2pojo Gradle plugin
This plugin is aiming to take raw JSON or YAML raw files or schemas and convert to Java or Scala POJOs (Plain Old Java Object).
Currently it suppots features like generating using Jackson, GSON and Mocha1 annotations and JSR305.
Please note, that JSR305 is purely supported, and present only if you don’t want to use a proper JSON validation for some reasons.
plugins {
id "org.jsonschema2dataclass" version "$latestRelease"
}
Please refer Gradle Plugins page for further details.
Usage for Android and for Java is basically the same. You need to declare plugin usage and configure plugin parameters if needed.
To execute just a java classes generation you need to run generateJsonSchema2DataClass
gradle task. Otherwise, compileJava
and appropriate android tasks are already dependent to this plugin.
To start using a plugin you need follow this recipe:
plugins{
id "org.jsonschema2dataclass" version "$latestRelease"
}
jsonschema2pojo {
targetPackage = 'org.example.api' // specify package for your needs
source.setFrom files("${project.rootDir}/src/main/resources/json")
// ... — add other supported options as you need
}
Additionally you can copy one of sample Java and Android projects you can find in demo folder
Note
|
📝 Documentation is a bit outdated on the jsonschema2pojo pages. Help wanted for this task |
Caution
|
|
Name | Type | Default | Description |
---|---|---|---|
annotationStyle |
String |
jackson2 |
The style of annotations to use in the generated Java types. Supported values: jackson2 (apply annotations from the Jackson 2.x library) jackson (alias for jackson2) gson (apply annotations from the gson library) moshi1 (apply annotations from the moshi 1.x library) none (apply no annotations at all) |
classNamePrefix |
String |
Whether to add a prefix to generated classes. |
|
classNameSuffix |
String |
Whether to add a suffix to generated classes. |
|
constructorsRequiredPropertiesOnly |
boolean |
false |
The 'constructorsRequiredPropertiesOnly' configuration option. This is a legacy configuration option used to turn on the isIncludeAllPropertiesConstructor() and off the * #isConstructorsIncludeAllPropertiesConstructor()configuration options. It is specifically tied to the isIncludeConstructors() * property, and will do nothing if that property is not enabled |
customAnnotator |
String |
org.jsonschema2pojo.NoopAnnotator |
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. |
customDatePattern |
String |
A custom pattern to use when formatting date fields during serialization. Requires support from your JSON binding library. |
|
customDateTimePattern |
String |
A custom pattern to use when formatting date-time fields during serialization. Requires support from your JSON binding library. |
|
customRuleFactory |
String |
org.jsonschema2pojo.rules.RuleFactory |
A fully qualified class name, referring to an class that extends org.jsonschema2pojo.rules.RuleFactory and will be used to create instances of Rules used for code generation. |
customTimePattern |
String |
A custom pattern to use when formatting time fields during serialization. Requires support from your JSON binding library. |
|
dateTimeType |
String |
What type to use instead of string when adding string type fields of format date-time to generated Java types. |
|
dateType |
String |
What type to use instead of string when adding string type fields of format date (not date-time) to generated Java types. |
|
fileExtensions |
List<String> |
The strings (no preceeding dot) that should be considered as file name extensions, and therefore ignored, when creating Java class names. |
|
fileFilter |
FileFilter |
List of file patterns to exclude. This only applies to the initial scan of the file system and will not prevent inclusion through a "$ref" in one of the schemas. |
|
formatDateTimes |
boolean |
false |
Whether the fields of type |
formatDates |
boolean |
false |
Whether the fields of type |
formatTimes |
boolean |
false |
Whether the fields of type |
formatTypeMapping |
Map |
(no description) |
|
generateBuilders |
boolean |
false |
Whether to generate builder-style methods of the form withXxx(value) (that return this), alongside the standard, void-return setters. |
includeAdditionalProperties |
boolean |
true |
Whether to allow 'additional properties' support in objects. Setting this to false will disable additional properties support, regardless of the input schema(s). |
includeAllPropertiesConstructor |
boolean |
true |
The 'constructorsIncludeRequiredPropertiesConstructor' configuration option. This property works in collaboration with the isIncludeConstructors() configuration option and is incompatible with isConstructorsRequiredPropertiesOnly(), and will have no effect if isIncludeConstructors() is not set to true. If isIncludeConstructors() is set to true then this configuration determines whether the resulting object should include a constructor with all listed properties as parameters. |
includeConstructorPropertiesAnnotation |
boolean |
false |
(no description) |
includeConstructors |
boolean |
false |
Whether to generate constructors or not |
includeCopyConstructor |
boolean |
false |
The 'constructorsIncludeRequiredPropertiesConstructor' configuration option. This property works in collaboration with the isIncludeConstructors() configuration option and is incompatible with isConstructorsRequiredPropertiesOnly(), and will have no effect if isIncludeConstructors() is not set to true. If isIncludeConstructors() is set to true then this configuration determines whether the resulting object should include a constructor the class itself as a parameter, with the expectation that all properties from the originating class will assigned to the new class. |
includeDynamicAccessors |
boolean |
Whether to include dynamic getters, setters, and builders or to omit these methods. |
|
includeDynamicBuilders |
boolean |
false |
Whether to include dynamic builders or to omit these methods. |
includeDynamicGetters |
boolean |
false |
Whether to include dynamic getters or to omit these methods. |
includeDynamicSetters |
boolean |
false |
Whether to include dynamic setters or to omit these methods. |
includeGeneratedAnnotation |
boolean |
false |
(no description) |
includeGetters |
boolean |
true |
Whether to include getters or to omit this accessor method and create public fields instead |
includeHashcodeAndEquals |
boolean |
true |
Whether to include hashCode and equals methods in generated Java types. |
includeJsr303Annotations |
boolean |
false |
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. |
includeJsr305Annotations |
boolean |
false |
Whether to include JSR-305 annotations (for schema rules like Nullable, NonNull, etc) in generated Java types. |
includeRequiredPropertiesConstructor |
boolean |
false |
The 'constructorsIncludeRequiredPropertiesConstructor' configuration option. This property works in collaboration with the isIncludeConstructors() configuration option and is incompatible with isConstructorsRequiredPropertiesOnly(), and will have no effect if isIncludeConstructors() is not set to true. If isIncludeConstructors() is set to true then this configuration determines whether the resulting object should include a constructor with only the required properties as parameters. |
includeSetters |
boolean |
true |
Whether to include setters or to omit this accessor method and create public fields instead |
includeToString |
boolean |
true |
Whether to include a toString method in generated Java types. |
includeTypeInfo |
boolean |
false |
Whether to include json type information; often required to support polymorphic type handling. By default the type information is stored in the @class property, this can be overridden in the deserializationClassProperty of the schema. |
inclusionLevel |
String |
NON_NULL |
The Level of inclusion to set in the generated Java types for Jackson serializers. Supported values ALWAYS NON_ABSENT NON_DEFAULT NON_EMPTY NON_NULL USE_DEFAULTS |
initializeCollections |
boolean |
true |
Whether to initialize Set and List fields as empty collections, or leave them as null. |
outputEncoding |
String |
UTF-8 |
The character encoding that should be used when writing the generated Java source files. |
parcelable |
boolean |
false |
EXPERIMENTAL Whether to make the generated types 'parcelable' (for Android development). |
propertyWordDelimiters |
String |
- _ |
The characters that should be considered as word delimiters when creating Java Bean property names from JSON property names. If blank or not set, JSON properties will be considered to contain a single word when creating Java Bean property names. |
refFragmentPathDelimiters |
String |
#/. |
A string containing any characters that should act as path delimiters when resolving $ref fragments. By default, #, / and . are used in an attempt to support JSON Pointer and JSON Path. |
removeOldOutput |
boolean |
false |
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. |
serializable |
boolean |
Whether to make the generated types 'serializable'. |
|
source |
ConfigurableFileCollection |
Location of the JSON Schema file(s). Note: this may refer to a single file or a directory of files. |
|
sourceSortOrder |
String |
OS |
The sort order to be applied when recursively processing the source files. By default the OS can influence the processing order. Supported values: OS (Let the OS influence the order the source files are processed.) FILES_FIRST (Case sensitive sort, visit the files first. The source files are processed in a breadth first sort order.) SUBDIRS_FIRST (Case sensitive sort, visit the sub-directories before the files. The source files are processed in a depth first sort order.) |
sourceType |
String |
jsonschema |
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) yamlschema (JSON schema documents, represented as YAML) yaml (documents that represent an example of the kind of YAML (or JSON) data that the generated Java types will be mapped to) |
targetDirecotryPrefix |
String |
$buildDir/generated/source/js2d |
Directory prefix under build directory where underlying tool will generate sources |
targetPackage |
String |
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). |
|
targetVersion |
String |
The target version for generated source files. |
|
timeType |
String |
What type to use instead of string when adding string type fields of format time (not date-time) to generated Java types. |
|
toStringExcludes |
List<String> |
The fields to be excluded from toString generation |
|
useBigDecimals |
boolean |
false |
Whether to use the java type BigDecimal instead of float (or Float) when representing the JSON Schema type 'number'. Note that this configuration overrides isUseDoubleNumbers(). |
useBigIntegers |
boolean |
false |
Whether to use the java type BigInteger instead of int (or Integer) when representing the JSON Schema type 'integer'. Note that this configuration overrides isUseLongIntegers(). |
useDoubleNumbers |
boolean |
true |
Whether to use the java type double (or Double) instead of float (or Float) when representing the JSON Schema type 'number'. |
useInnerClassBuilders |
boolean |
false |
If set to true, then the gang of four builder pattern will be used to generate builders on generated classes. Note: This property works in collaboration with the isGenerateBuilders() method. If the isGenerateBuilders() is false, then this property will not do anything. |
useJodaDates |
boolean |
false |
Whether to use DateTime instead of Date when adding date type fields to generated Java types. |
useJodaLocalDates |
boolean |
false |
Whether to use LocalDate instead of string when adding string type fields of format date (not date-time) to generated Java types. |
useJodaLocalTimes |
boolean |
false |
Whether to use LocalTime instead of string when adding string type fields of format time (not date-time) to generated Java types. |
useLongIntegers |
boolean |
false |
Whether to use the java type long (or Long) instead of int (or Integer) when representing the JSON Schema type 'integer'. |
useOptionalForGetters |
boolean |
false |
Whether to use Optional as return type for getters of non-required fields. |
usePrimitives |
boolean |
false |
Whether to use primitives (long, double, boolean) instead of wrapper types where possible when generating bean properties (has the side-effect of making those properties non-null). |
useTitleAsClassname |
boolean |
false |
Use the title as class name. Otherwise, the property and file name is used. |