Generating POJOs from JSON Schema

There are lots of tools to generate POJOs from XML Schema files (XSD) and even DTDs. This is helpful for mapping XML directly to Java classes automatically, e.g. by using JAXB.

Recently I wanted to do the same for JSON and stumbled upon a neat tool called jsonschema2pojo. On their website they have an easy to use Javascript app that allows you to quickly generate POJOs from JSON Schema and even from JSON itself.

What makes this tool really awesome is it's support for both of the most widely used JSON mappers in the Java world, namely Jackson and GSON. jsonschema2pojo is capable of annotating the generated POJOs for both libraries. There are some other useful features such as support for commons-3 when generating e.g. toString and generating builder pattern methods.

To me, the most useful features are plugins for all commonly used build tools for Java, i.e. you can integrate the generation of you POJOs into your build process using Ant, Maven and Gradle.

A typical maven integration of the plugin for Jackson2 looks as follows:

<plugin>  
    <groupId>org.jsonschema2pojo</groupId>
        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
        <version>${jsonschema2pojo-maven-plugin.version}</version>
        <configuration>
            <sourceType>jsonschema</sourceType>
            <outputEncoding>${project.build.sourceEncoding}</outputEncoding>
            <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
            <annotationStyle>jackson2</annotationStyle>
            <generateBuilders>true</generateBuilders>
            <initializeCollections>true</initializeCollections>
        </configuration>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <targetPackage>mypackage</targetPackage>
                <sourceDirectory>${basedir}/src/main/resources/package/json/schema</sourceDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>  

If you generate classes from multiple schema files, chances are that some of them specify objects with the same name. When jsonschema2pojo generates these to the same package it solves this by naming them Myobject.java, Myobject_.java and so forth. To solve this, configure the plugin with multiple executions each with custom configuration of source directory and target package:

<plugin>  
    <groupId>org.jsonschema2pojo</groupId>
        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
        <version>${jsonschema2pojo-maven-plugin.version}</version>
        <configuration>
            <sourceType>jsonschema</sourceType>
            <outputEncoding>${project.build.sourceEncoding}</outputEncoding>
            <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
            <annotationStyle>jackson2</annotationStyle>
            <generateBuilders>true</generateBuilders>
            <initializeCollections>true</initializeCollections>
        </configuration>
    <executions>
        <execution>
            <id>generate-first</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <targetPackage>mypackage.first</targetPackage>
                <sourceDirectory>${basedir}/src/main/resources/package/json/schema/first</sourceDirectory>
            </configuration>
        </execution>
        <execution>
            <id>generate-second</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <targetPackage>mypackage.second</targetPackage>
                <sourceDirectory>${basedir}/src/main/resources/package/json/schema/second</sourceDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>  

I applied this technique in my Java library for the Steam web API in Java which is provided by Valve. Check it out for a live example!

Lukas

Read more posts by this author.

Dortmund, Germany https://lukaspradel.com

Subscribe to Programming & Whisky

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!