jsonschema2pojo
Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
Quick Overview
jsonschema2pojo is a Java library and Maven plugin that generates Java POJOs (Plain Old Java Objects) from JSON Schema definitions. It simplifies the process of creating Java classes that represent JSON data structures, making it easier to work with JSON in Java applications.
Pros
- Automates the creation of Java classes from JSON Schema, saving development time
- Supports various JSON Schema formats and can generate annotations for popular libraries like Jackson and Gson
- Offers customization options through configuration settings and custom rule classes
- Integrates well with Maven and Gradle build processes
Cons
- Generated code may not always be optimal or follow best practices for complex schemas
- Limited control over the generated class structure without custom rule implementations
- May require manual adjustments for edge cases or specific requirements
- Learning curve for advanced customization and rule creation
Code Examples
- Basic usage with Maven plugin:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
This Maven configuration generates Java classes from JSON Schema files in the specified source directory.
- Programmatic usage:
import org.jsonschema2pojo.*;
import org.jsonschema2pojo.rules.*;
GenerationConfig config = new DefaultGenerationConfig();
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
mapper.generate(codeModel, "ClassName", "com.example", "schema.json");
This code snippet demonstrates how to use jsonschema2pojo programmatically to generate Java classes from a JSON Schema file.
- Custom rule implementation:
public class CustomStringRule implements Rule<JClassContainer, JType> {
@Override
public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container, Schema schema) {
JType stringType = container.owner().ref(String.class);
JFieldVar field = container.getOwner().field(JMod.PRIVATE, stringType, nodeName);
field.annotate(Pattern.class).param("regexp", "^[A-Z]{3}$");
return stringType;
}
}
This example shows a custom rule implementation that adds a regex pattern annotation to string fields.
Getting Started
To use jsonschema2pojo in your Maven project:
- Add the plugin to your
pom.xml
:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
- Place your JSON Schema files in
src/main/resources/schema
. - Run
mvn generate-sources
to generate Java classes. - Use the generated classes in your project.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
jsonschema2pojo
jsonschema2pojo generates Java types from JSON Schema (or example JSON) and can annotate those types for data-binding with Jackson 2.x or Gson.
Try jsonschema2pojo online
or brew install jsonschema2pojo
You can use jsonschema2pojo as a Maven plugin, an Ant task, a command line utility, a Gradle plugin or embedded within your own Java app. The Getting Started guide will show you how.
A very simple Maven example:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
A very simple Gradle example:
plugins {
id "java"
id "org.jsonschema2pojo" version "1.2.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
}
jsonSchema2Pojo {
targetPackage = 'com.example'
}
Useful pages:
- Getting started
- How to contribute
- Reference
- Latest Javadocs
- Documentation for the Maven plugin
- Documentation for the Gradle plugin
- Documentation for the Ant task
Project resources:
Special thanks:
- unkish
- Thach Hoang
- Dan Cruver
- Ben Manes
- Sam Duke
- Duane Zamrok
- Christian Trimble
- YourKit, who support this project through a free license for the YourKit Java Profiler.
Licensed under the Apache License, Version 2.0.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot