从 java 类生成 JSON 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26199716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate JSON schema from java class
提问by user3587174
I have a POJO class
我有一个 POJO 课程
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
是否有任何注释或开发框架/api 可以将 POJO 转换为 JSON 模式,如下所示
{"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.
并且我还可以通过在 POJO 上指定一些注释或配置来扩展架构以添加诸如“必需”:“是”、每个字段的描述等信息,并且可以生成如下所示的 JSON 架构。
{"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"
}
}
回答by StormeHawke
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
我遇到了自己需要这样做的需要,但需要获取最新的架构规范(本文中的 v4)。我的解决方案是以下链接中的第一个答案: Generate Json Schema from POJO with a twist
Use objects from the org.codehaus.Hymanson.mappackage rather than the com.fasterxml.Hymanson.databindpackage. If you're following the instructions on thispage then you're doing it wrong. Just use the Hymanson-mappermodule instead.
使用org.codehaus.Hymanson.map包中的对象而不是com.fasterxml.Hymanson.databind包中的对象。如果您按照此页面上的说明进行操作,那么您就做错了。只需使用该Hymanson-mapper模块即可。
Here's the code for future googlers:
这是未来谷歌员工的代码:
private static String getJsonSchema(Class clazz) throws IOException {
org.codehaus.Hymanson.map.ObjectMapper mapper = new ObjectMapper();
//There are other configuration options you can set. This is the one I needed.
mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
JsonSchema schema = mapper.generateJsonSchema(clazz);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
}
回答by StaxMan
One such tool is Hymanson JSON Schema module:
Hymanson JSON Schema 模块就是这样一种工具:
https://github.com/FasterXML/Hymanson-module-jsonSchema
https://github.com/FasterXML/Hymanson-module-jsonSchema
which uses Hymanson databind's POJO introspection to traverse POJO properties, taking into account Hymanson annotations, and produces a JSON Schema object, which may then be serialized as JSON or used for other purposes.
它使用 Hymanson databind 的 POJO 自省来遍历 POJO 属性,同时考虑 Hymanson 注释,并生成一个 JSON Schema 对象,然后可以将其序列化为 JSON 或用于其他目的。
回答by Drona
Use JJschema. It can generate draft 4 compliant JSON schemas. Refer this post http://wilddiary.com/generate-json-schema-from-java-class/for details.
使用 JJschema。它可以生成符合草案 4 的 JSON 模式。有关详细信息,请参阅此帖子http://wilddiary.com/generate-json-schema-from-java-class/。
Though Hymanson Json Schema modulecan too generate schema but it can, as of today, only generate draft 3 compliant schemas only.
虽然Hymanson Json Schema 模块也可以生成模式,但截至今天,它只能生成符合草案 3 的模式。
回答by P Rajesh
public static String getJsonSchema(Class clazz) throws IOException {
Field[] fields = clazz.getDeclaredFields();
List<Map<String,String>> map=new ArrayList<Map<String,String>>();
for (Field field : fields) {
HashMap<String, String> objMap=new HashMap<String, String>();
objMap.put("name", field.getName());
objMap.put("type", field.getType().getSimpleName());
objMap.put("format", "");
map.add(objMap);
}
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
return json;
}

