33

My problem is that i am serializing the content of map to JSON.

In the output (JSON), i have object that follow key/name syntax rule.

The key is created from map key, and the name from the value.

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:

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

JSON Schema:

{
  "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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • sounds like the properties you have would be an array of "key": "value": < value value>. – ErstwhileIII Mar 06 '14 at 15:48
  • I do not want to have an array of pairs. I would like to have the key=value as the part of object that is valid to have and easy to deserialize. – Damian Leszczyński - Vash Mar 06 '14 at 15:50
  • So you are asking how to specify name, value pairs for a situation in which you do not know the names a priori? Can't be done. Anyway, isn't your goal to be able to reconstitute the HashMap? In that case you would go through a loop adding Key, value anyway. – ErstwhileIII Mar 06 '14 at 16:01
  • You might be right. But if there is a valid way to serialize and deserialize such object is should be possible to create a Schema for it. To advise people that can expect some object with undefined interiors. – Damian Leszczyński - Vash Mar 06 '14 at 16:07

2 Answers2

31

Assuming your validator supports it you can use 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.

Florent Hemmi
  • 105
  • 1
  • 2
  • 8
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 15
    Even simpler: use `"additionalProperties": { "type": "string" }`. This specifies that any properties not otherwise specified must satisfy the given schema. – augurar Apr 11 '16 at 18:18
  • ".{1,}" Using this solution for data with Map key values provided the answer to my problem. I'm glad you posted. Thank you. – jimmy Mar 20 '21 at 23:05
1

I used the Solution suggested by @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

@Arne Burmeister - in my case - Solution 1 didnt worked as needed - although it didnt gave any error in the Model (Schema Created)

Abdeali Chandanwala
  • 8,449
  • 6
  • 31
  • 45