Java 从 Json Schema 生成示例 Json 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21894873/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:18:33  来源:igfitidea点击:

Generate sample Json output from Json Schema

javajson

提问by Abhijeet Kushe

I want to know whether there is a method in which I can generate sample json output based on a json schema input.

我想知道是否有一种方法可以根据 json 模式输入生成示例 json 输出。

for example :-

例如 :-

input =>

{
"title": "Example Schema",
"type": "object",
"properties": {
    "firstName": {
        "type": "string"
    },
    "lastName": {
        "type": "string"
    },
    "age": {
        "description": "Age in years",
        "type": "integer",
        "minimum": 0
    }
},
"required": ["firstName", "lastName"]
}

output => 

{     
  "firstName" : "RandomFirstName",
   "lastName" : "RandomLastName"
}

I have a large Json Schema with plenty of validations so to generate a sample valid json I could either create one manually using either Java or just a type it into a file. Is there a better way available ?

我有一个包含大量验证的大型 Json Schema,因此要生成一个示例有效 json,我可以使用 Java 手动创建一个,也可以仅将其输入到文件中。有没有更好的方法?

采纳答案by Eric Olson

You can try the JSON Schema Faker. It will take a schema and generate/output a JSON object that will validate against the schema.

您可以尝试JSON Schema Faker。它将采用模式并生成/输出将针对模式进行验证的 JSON 对象。

回答by Clemens

JSONBuddycan do this for you. It is a Windows desktop JSON editor and generates live JSON sample data while you are editing your schema.

JSONBuddy可以为您做到这一点。它是一个 Windows 桌面 JSON 编辑器,可在您编辑架构时生成实时 JSON 示例数据。

回答by Ted_Zactly

You can also use the ModelObject in Adobe Ride (full disclosure: self-plug here). Point the ModelObject (or a subclass thereof) to a schema in your java project resources: https://github.com/adobe/ride/blob/develop/sample/sample-service-extension/src/test/java/com/adobe/ride/sample/tests/ObjectCreation.java#L38

您还可以在 Adob​​e Ride 中使用 ModelObject(完整披露:此处自插)。将 ModelObject(或其子类)指向 Java 项目资源中的模式:https: //github.com/adobe/ride/blob/develop/sample/sample-service-extension/src/test/java/com/ adobe/ride/sample/tests/ObjectCreation.java#L38

You can also use the Ride Fuzzer Lib to easily tests sending negative data into the schema nodes (based on an array of OWASP and google injection test strings, and other various types data): https://github.com/adobe/ride/tree/develop/libraries/ride-fuzzer-lib

您还可以使用 Ride Fuzzer Lib 轻松测试将负面数据发送到架构节点(基于 OWASP 和 google 注入测试字符串数组以及其他各种类型的数据):https: //github.com/adobe/ride/树/开发/库/ride-fuzzer-lib

All Ride modules are open source and free: https://github.com/adobe/ride/

所有 Ride 模块都是开源且免费的:https: //github.com/adobe/ride/