Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JSON schema from Java class

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"
      }
}
like image 701
user3587174 Avatar asked Oct 05 '14 04:10

user3587174


3 Answers

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.

like image 62
StaxMan Avatar answered Nov 03 '22 20:11

StaxMan


Java JSON Schema Generator: https://github.com/victools/jsonschema-generator

Creates JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from Java classes using Jackson.

like image 29
Edgar Domingues Avatar answered Nov 03 '22 18:11

Edgar Domingues


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.

like image 2
Drona Avatar answered Nov 03 '22 18:11

Drona