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?
Asked
Active
Viewed 3.0k times
14
-
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. – user1983983 Jul 22 '13 at 09:33
2 Answers
29
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"
}
}
}
}
}
}

simon04
- 3,054
- 29
- 25

Michał Ziober
- 37,175
- 18
- 99
- 146
-
You have 2 "type" keys in "id" object! Is it correct? Can you please explain? Thanks – thermz Mar 05 '14 at 14:24
-
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`. – Michał Ziober Mar 07 '14 at 10:29
-
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: http://www.jsonschema.net/ – thermz Mar 07 '14 at 14:41
-
@MichałZiober i am getting "id" property on top level object .. do you know how to remove that? This is my question : https://stackoverflow.com/questions/68231913/how-to-remove-bakslash-for-quotes-from-jsonschema-jackson?noredirect=1#comment120591154_68231913 – MrSham Jul 06 '21 at 17:48
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"}
}
}
-
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. – Joe Jun 08 '16 at 23:00