json - 从 Java 类生成 JSON 模式

标签 json jackson jsonschema fasterxml jackson-modules

我有一个 POJO 类:

public class Stock {
 int id;
 String name;
 Date date;
}

是否有任何注释或开发框架/API 可以将 POJO 转换为 JSON 模式,如下所示:

{"id":
      {             
        "type" : "int"
      },
"name":{   
        "type" : "string"
       }
"date":{
        "type" : "Date"
      }
}

我还可以扩展模式以添加信息,如“必需”:"is",每个字段的描述等,通过在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"
      }
}

最佳答案

Jackson JSON Schema 模块就是一个这样的工具:

https://github.com/FasterXML/jackson-module-jsonSchema

它使用 Jackson 数据绑定(bind)的 POJO 自省(introspection)来遍历 POJO 属性,同时考虑到 Jackson 注释,并生成 JSON Schema 对象,然后可以将其序列化为 JSON 或用于其他目的。

关于json - 从 Java 类生成 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199716/

相关文章:

java - 将手动创建的 JSONObject 的内容传递给 Jackson

java - moshi 序列化支持,如 @jsonunwrapped

java - 无法在java中通过JNDI连接MongoDb

javascript - 如何为 jsonschema 生成时间字符串

json - swift : The data couldn’t be read because it isn’t in the correct format

javascript - Angularjs 不会从外部 json 文件中读取

json - JSON 模式中的变量属性

MySQL bigInt 与 JSON 模式整数

c# - 使用自定义 Newtonsoft JSON 转换器解析具有重复键的 JSON

JavaScript json - 填充输入框不起作用