简体   繁体   English

如何为Name / Value结构创建JSON Schema?

[英]How to create JSON Schema for Name/Value structure?

My problem is that i am serializing the content of map to JSON. 我的问题是我正在将地图内容序列化为JSON。

In the output (JSON), i have object that follow key/name syntax rule. 在输出(JSON)中,我有跟随键/名称语法规则的对象。

The key is created from map key, and the name from the value. 密钥是从map密钥创建的,名称来自值。

Model Example: 模型示例:

  class Storage {
       Map<String,String> values = new HashMap<>();

      {
         map.put("key1","key1");
         map.put("key2","key2");
         map.put("key3","key3");
      }

    }

JSON Example object: JSON示例对象:

{
  key1=value1,
  key2=value2,
  key3=value3
}

JSON Schema: JSON架构:

{
  "name": "storage",
  "description": "Store of key values",
  "properties": {
    // How can we describe the properties if we do not know the name ?
   }
}

The issue is that i do not know what the values will be but i know that they will be some. 问题是,我不知道它的价值是什么,但我知道它们会是一些。

Can you help me to provide me the full definition of schema? 你能帮我提一下架构的完整定义吗?


Disclaimer: 免责声明:

I know that this can be also serialized as 我知道这也可以序列化为

 {
    values: [
       {key="key1", value="value1"},
       {key="key2", value="value2"},
       {key="key3", value="value3"}
    ]
 }

but is do not want to have array in the JSON. 但是不希望在JSON中有数组。

Assuming your validator supports it you can use patternProperties . 假设您的验证器支持它,您可以使用patternProperties

For the schema... 对于架构......

{
  "title": "Map<String,String>",
  "type": "object",
  "patternProperties": {
    ".{1,}": { "type": "string" }
  }
}

...and the document... ......和文件......

{
    "foo":"bar",
    "baz":1
}

...the value of property foo is valid because it is a string but baz fails validation because it is a number. ...属性foo的值是有效的,因为它是一个字符串,但是baz验证失败,因为它是一个数字。

I used the Solution suggested by @augurar "additionalProperties": { "type": "string" } 我使用了@augurar“additionalProperties”建议的解决方案:{“type”:“string”}

for AWS API Gateway Model .... and the SDK was able to generate the Map variable as required in Java / Android SDK 对于AWS API Gateway Model ....并且SDK能够根据Java / Android SDK中的需要生成Map变量

@Arne Burmeister - in my case - Solution 1 didnt worked as needed - although it didnt gave any error in the Model (Schema Created) @Arne Burmeister - 在我的情况下 - 解决方案1没有按需工作 - 尽管它没有在模型中给出任何错误(Schema Created)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM