I have a POJO class
public class Stock{
int id;
String name;
Date date;
}
Are there any annotations or development framework/api that can convert POJO to JSON schema like below
{"id":
{
"type" : "int"
},
"name":{
"type" : "string"
}
"date":{
"type" : "Date"
}
}
and also i can expand the schema to add information like "Required" : "Yes", description for each field, etc., by specifying some annotations or configurations on POJO and can generate JSON Schema like below.
{"id":
{
"type" : "int",
"Required" : "Yes",
"format" : "id must not be greater than 99999",
"description" : "id of the stock"
},
"name":{
"type" : "string",
"Required" : "Yes",
"format" : "name must not be empty and must be 15-30 characters length",
"description" : "name of the stock"
}
"date":{
"type" : "Date",
"Required" : "Yes",
"format" : "must be in EST format",
"description" : "filing date of the stock"
}
}
Use JJschema. It can generate draft 4 compliant JSON schemas. Refer this post http://wilddiary.com/generate-json-schema-from-java-class/ for details.
Though Jackson Json Schema module can too generate schema but it can, as of today, only generate draft 3 compliant schemas only.
I ran into a need to do this myself, but needed to get the latest schema spec (v4 as of this post). My solution is the first answer at the link below: Generate Json Schema from POJO with a twist
Use objects from the
org.codehaus.jackson.map
package rather than thecom.fasterxml.jackson.databind
package. If you're following the instructions on this page then you're doing it wrong. Just use thejackson-mapper
module instead.Here's the code for future googlers:
One such tool is Jackson JSON Schema module:
https://github.com/FasterXML/jackson-module-jsonSchema
which uses Jackson databind's POJO introspection to traverse POJO properties, taking into account Jackson annotations, and produces a JSON Schema object, which may then be serialized as JSON or used for other purposes.