How to describe a Map type in json schema?

15,008 views
Skip to first unread message

Ron MI

unread,
Jul 11, 2011, 11:14:10 PM7/11/11
to json-...@googlegroups.com
I am build a tool that can convert a Java data type into json schema style automatically. Everything goes well except the Map type.
I saw we can use the object type with properties in json schema to describe a map, so we can store the Entity<Key,Value> into the properties. But I saw we can only use string:object in current draft while the Key may be also in some object types, so How to describe it? Does it support schema as follows:

{

 "id":"person$1",

 "description":"A person",
 "type":"object",

 "properties":
  {

   {"type":"string"}: {"type":"string"},
   
{"type":{"$ref":"person$1"}} : {"type":"integer",

     "maximum":125}

  }
}

Paul C. Bryan

unread,
Jul 12, 2011, 11:54:14 AM7/12/11
to json-...@googlegroups.com
The JSON specification only allows for String keys in objects. How is your implementation mapping non-String-keys in JSON objects?

Paul

--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To view this discussion on the web visit https://groups.google.com/d/msg/json-schema/-/1gfH4Zcj9koJ.
To post to this group, send email to json-...@googlegroups.com.
To unsubscribe from this group, send email to json-schema...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.

fge

unread,
Jul 12, 2011, 7:47:25 PM7/12/11
to JSON Schema
As far as I can understand JSON, and the schema draft, your example
won't work because JSON itself will not allow anything else than
strings as keys, so your best bet here would be to define two schemas:

* one array schema for the [key, value] type, which is a two-members-
only array, the first member being the key and the second member being
the value (unlike for objects, array members _are_ ordered);
* another array schema with only items defined, with maxItems.

So: (with comments, note that comments in JSON are _illegal_, or at
least not defined enough so that they are safe):

----
{
"type": "array",
"maxItems": 125,
"items": {
"type": "array",
"items": [
{ // key schema goes here },
{ // value schema goes here }
],
"additionalItems": false
}
}
----

Two notes:

* the inner schema does item tuple validation - element 0 must match
schema 0 (the key), element 1 must match schema 1 (the value) - this
is because the "items" value is an array;
* the inner schema specifies additionalItems to be FALSE, which means
the array specified MUST have only these two elements;
* in the outer schema, the value of items is an object: this means the
only constraint is that elements in the array match the specified
schema, there is no (lower or upper) limit as to the number of items;
if you want items in the array to be unique, you may want to add
"uniqueItems": true as well.

<ad>My current JSON schema implementation will be able to validate
this as long as it doesn't do $ref, so please try it an report
bugs :p</ad>

fge

unread,
Jul 12, 2011, 7:49:32 PM7/12/11
to JSON Schema
[...]
> * in the outer schema, the value of items is an object: this means the
> only constraint is that elements in the array match the specified
> schema, there is no (lower or upper) limit as to the number of items;

... except the ones specified by minItems or maxItems, of course!

Ron MI

unread,
Jul 12, 2011, 9:52:30 PM7/12/11
to json-...@googlegroups.com
I saw some implementation. in Gson, it convert the object key into string like this:
"Person [id\u003d10000, name\u003dperson name]"
maybe it is not a standard or an stated rule in json schema, but it seems I have to do it like this, is it? 

Ron MI

unread,
Jul 12, 2011, 10:00:32 PM7/12/11
to json-...@googlegroups.com
it seems describing a map in array is a quick solution for this problem, but will it involve some ambiguous for developer that get this schema for the first time?
Do you have some solution to serialize/deserialize the object key into/from string object?
Reply all
Reply to author
Forward
0 new messages