Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON schema from Java class

Tags:

java

json

gson

I am using Gson to serialize/deserialize java objects to json. I want to display it in UI, and needs a schema to make a better description. This will allow me to edit objects and add more data than there actually is.
Can Gson provide json schema?
Does any other framework has that capability?

like image 956
oshai Avatar asked Jul 22 '13 09:07

oshai


2 Answers

Gson library probably does not contain any feature like that but you can try to solve your problem with Jackson library and jackson-module-jsonSchema module. For example, for below classes:

class Entity {

    private Long id;
    private List<Profile> profiles;

    // getters/setters
}

class Profile {

    private String name;
    private String value;
    // getters / setters
}

this program:

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(Entity.class, visitor);
        JsonSchema schema = visitor.finalSchema();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
    }
}

Prints below schema:

{
  "type" : "object",
  "properties" : {
    "id" : {
      "type" : "integer"
    },
    "profiles" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "name" : {
            "type" : "string"
          },
          "value" : {
            "type" : "string"
          }
        }
      }
    }
  }
}
like image 148
Michał Ziober Avatar answered Sep 20 '22 19:09

Michał Ziober


Have a look at JSONschema4-mapper project. With following setup:

SchemaMapper schemaMapper = new SchemaMapper();
JSONObject jsonObject = schemaMapper.toJsonSchema4(Entity.class, true);
System.out.println(jsonObject.toString(4));

you get following JSON schema for classes mentioned in Michal Ziober's answer to this question:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "additionalProperties": false,
    "type": "object",
    "definitions": {
        "Profile": {
            "additionalProperties": false,
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "value": {"type": "string"}
            }
        },
        "long": {
            "maximum": 9223372036854775807,
            "type": "integer",
            "minimum": -9223372036854775808
        }
    },
    "properties": {
        "profiles": {
            "type": "array",
            "items": {"$ref": "#/definitions/Profile"}
        },
        "id": {"$ref": "#/definitions/long"}
    }
}
like image 40
Maciej Avatar answered Sep 21 '22 19:09

Maciej