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?
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"
}
}
}
}
}
}
4 Comments
thermz
You have 2 "type" keys in "id" object! Is it correct? Can you please explain? Thanks
Michał Ziober
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.thermz
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
MrSham
@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/…
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
Joe
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.
gsoncan convert Java-Classes to JSON-format, that's whatgsondoes.