Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.
Shaun Francis edited this page Sep 27, 2017 · 11 revisions

Generator for domain event POJOs defined in json schemas.

Overview

jsonschema-pojo-generator is a maven plugin used to generate POJOs from a json schema and provides the following:

  • Creates java POJOs from json schema documents
  • All generated POJOs can be parsed to and from json using libraries such as Jackson or Gson
  • No annotations on the generated POJO so not tied to one particular library
  • Maven plugin - generation is part of the build
  • Class generation can be modified using Plugins
  • Can override generation by writing your own version of a POJO

So given a json schema file mycontext.events.recipe-added.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "",
  "type": "object",
  "properties": {
    "recipeId": {
      "id": "/recipeId",
      "type": "string",
      "description": "Uniquely identifies the recipe",
      "name": "id of recipe",
      "title": "id of recipe"
    },
    "name": {
      "id": "/name",
      "type": "string",
      "description": "Name of the recipe",
      "name": "Name of Cake",
      "title": "Name of Cake"
    },
    "glutenFree": {
      "id": "/glutenFree",
      "type": "boolean"
    },
    "ingredients": {
      "id": "/ingredients",
      "type": "array",
      "items": [
        {
          "id": "ingredient",
          "type": "object",
          "properties": {
            "name": {
              "id": "name",
              "type": "string"
            },
            "quantity": {
              "id": "quantity",
              "type": "integer"
            }
          }
        }
      ],
      "minItems": 1,
      "description": "List ingredients and quantities for recipe"
    }
  },
  "required": [
    "name",
    "ingredients"
  ]
}

will generate two java classes:

RecipeAdded

public class RecipeAdded {
  private final String name;

  private final Boolean glutenFree;

  private final List<Ingredients> ingredientsList;

  private final String recipeId;

  public RecipeAdded(
  		final String name, 
  		final Boolean glutenFree, 
  		final List<Ingredients> ingredientsList, 
  		final String recipeId) {
    this.name = name;
    this.glutenFree = glutenFree;
    this.ingredientsList = ingredientsList;
    this.recipeId = recipeId;
  }

  public String getName() {
    return name;
  }

  public Boolean getGlutenFree() {
    return glutenFree;
  }

  public List<Ingredients> getIngredientsList() {
    return ingredientsList;
  }

  public String getRecipeId() {
    return recipeId;
  }
}   

Ingredients

public class Ingredients {
    private final Integer quantity;

    private final String name;

    public Ingredients(final Integer quantity, final String name) {
        this.quantity = quantity;
        this.name = name;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public String getName() {
        return name;
    }
}

Usage

Quirks/Caveats

  • Arrays must be of the same type
  • Enums must be Strings (empty strings are supported with BLANK enumeration)
  • ‘not’ not supported