jsonschema2pojo

repository·master·Indexed 27 days ago

https://github.com/joelittlejohn/jsonschema2pojo

A tool that generates Java POJOs from JSON Schema or example JSON files. It provides annotations for data-binding libraries such as Jackson (2.x, 3.x) and Gson. The tool is available as a Maven plugin, Gradle plugin, command line utility, or as a library to be embedded within a Java application.

Tokens
2.8K
Snippets
6
Records
22
Agent score
83%

What's inside jsonschema2pojo

  1. Overview of jsonschema2pojo

    master
    jsonschema2pojo generates Java types from JSON Schema or example JSON. It can annotate these generated types for data-binding with Jackson 2.x, Jackson 3.x, or Gson. It is available as a Maven plugin, a Gradle plugin, a command line utility, or can be embedded within a Java application.
  2. Test local changes to the Gradle plugin

    master

    To test local modifications of the jsonschema2pojo-gradle-plugin in an existing Gradle project, follow these steps:

    1. Build and install the plugin to your local Maven repository from the project root using ./mvnw clean install.
    2. Configure your target project's build.gradle to use mavenLocal() in both the buildscript and the project repositories blocks.
    3. Update the plugin dependency to use the latest.integration version.
    buildscript {
        repositories {
            mavenLocal()
        }
        dependencies {
            classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:latest.integration'
        }
    }
    
    repositories {
        mavenLocal()
    }
  3. Use the generateJsonSchema2Pojo Gradle task

    master

    The generateJsonSchema2Pojo task is automatically triggered in projects containing a jsonSchema2Pojo configuration closure. When executed, it performs the following actions:

    1. Invokes the jsonschema2pojo generator.
    2. Makes the compileJava task dependent on this task.
    3. Adds the targetDirectory to the main/java source set so the Java compiler can locate and compile the generated source files.
  4. Use the jsonschema2pojo CLI

    master

    The jsonschema2pojo command line interface allows you to generate Java classes from JSON Schemas. Arguments are expected in POSIX format. To view all available options and details, invoke the command with the --help flag.

    If the application encounters paths that do not exist, it will throw a FileNotFoundException. If it cannot read data from the specified paths, it will throw an IOException.

  5. Configure the jsonschema2pojo Gradle plugin

    master

    To use jsonschema2pojo in a Gradle project, apply the org.jsonschema2pojo plugin and configure the jsonSchema2Pojo extension block. You can set the targetPackage within this block.

    plugins {
      id "java"
      id "org.jsonschema2pojo" version "1.3.3"
    }
    
    repositories {
      mavenCentral()
    }
    
    jsonSchema2Pojo {
      targetPackage = 'com.example'
    }
  6. Configure the jsonschema2pojo-maven-plugin

    master

    To use jsonschema2pojo in a Maven project, add the jsonschema2pojo-maven-plugin to your pom.xml. You can configure the sourceDirectory where your schemas are located and the targetPackage for the generated classes.

    <plugin>
        <groupId>org.jsonschema2pojo</groupId>
        <artifactId>jsonschema2pojo-maven-plugin</artifactId>
        <version>1.3.3</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>