Jackson jsonSchema Generator /Kotlin
Generate JSON schema from your POJOs using Jackson @Annotations.
This project is a quick re-implementation of mbknor-jackson-jsonSchema by @mbknor, in Kotlin (Java 6 compatible, Android compatible).
Why? I wanted to use it in Android. While mbknor-jackson-jsonSchema can easily be modified to target Java 6, the Scala runtime is still quite an overhead.
Gradle (jcenter)
compile 'com.dr:ktjsonschema:1.0.2'Maven (jcenter)
<dependency>
<groupId>com.dr</groupId>
<artifactId>ktjsonschema</artifactId>
<version>1.0.2</version>
<type>pom</type>
</dependency>
Highlights
- JSON Schema Draft v4
- Supports polymorphism (@JsonTypeInfo, MixIn, and registerSubtypes()) using JsonSchema's oneOf-feature.
- Supports schema customization using:
- @JsonSchemaDescription/@JsonPropertyDescription
- @JsonSchemaFormat
- @JsonSchemaTitle
- @JsonSchemaDefault
- Supports many Javax-validation @Annotations
- Works well with Generated GUI's using https://github.com/jdorn/json-editor
- (Must be configured to use this mode)
- Supports custom class-to-format mapping
- Supports java.lang.Optional and java8.lang.Optional
Status
The translation from the Scala codebase was blind and quick, so some bugs may be present, though I haven't found any (yet).
Example (Java)
ObjectMapper objectMapper = new ObjectMapper();
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper);
// If using JsonSchema to generate HTML5 GUI:
// JsonSchemaGenerator html5 = new JsonSchemaGenerator(objectMapper, JsonSchemaConfig.getHtml5EnabledSchema() );
// If you want to configure it manually:
// JsonSchemaConfig config = new JsonSchemaConfig(...);
// JsonSchemaGenerator generator = new JsonSchemaGenerator(objectMapper, config);
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(YourPOJO.class);
String jsonSchemaAsString = objectMapper.writeValueAsString(jsonSchema);