14

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?

1
  • What would you like to display in UI? I don't really get what's the problem is. gson can convert Java-Classes to JSON-format, that's what gson does. Commented Jul 22, 2013 at 9:33

2 Answers 2

30

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"
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

You have 2 "type" keys in "id" object! Is it correct? Can you please explain? Thanks
I am not sure why we have these two types. I have to check. Do you have the similar output in this case? Why did you update my answer? I put schema which I received from SchemaFactoryWrapper.
Then that's a huge mistake!: the JSON Schema standard specify that a single property can have multiple types, but not in this way! That's the correct one: "type": ["number","integer"]. Having 2 keys in the same JSON is against every standard! Keys are unique. For more information: jsonschema.net
@MichałZiober i am getting "id" property on top level object .. do you know how to remove that? This is my question : stackoverflow.com/questions/68231913/…
8

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"}
    }
}

1 Comment

Great library, just wish I would work with JDK 7 and not 8 as the baseline. Will keep this in mind should we ever move to JDK8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.