Question

We are currently investigating JSON as a potential API data transfer language for our system and a question about using JSON Reference came up.

Consider the following example:

{
   "invoice-address" : { "street": "John Street", "zip": "12345", "city": "Someville" },
   "shipping-address": { "$ref": "#/invoice-address" }
}

According to our research, this is a valid usage of JSON Reference. We replace the instance of an object with another object containing the reference pointing to a different object using a JSON Pointer fragment.

Now, a JSON Reference always consists of a key-value pair and thus has to be enclosed in an object. This would mean that in order to reference a non-object data type (e.g. the zip and city strings in the example above) you would have to do the following:

{
   "invoice-address" : { "street": "John Street", "zip": "12345", "city": "Someville" },
   "shipping-address": { "street": "Doe Street", "zip": { "$ref": "#/invoice-address/zip" }, "city": { "$ref": "#/invoice-address/city" } }
}

Even though the JSON Pointers now correctly point to string values, we had to change the data type of zip and city from string to object, which make them fail validation against our JSON Schema, because it declares them as strings.

However, the JSON Reference draft states:

Implementations MAY choose to replace the reference with the referenced value.

Does that mean that we are allowed to "preprocess" the file and replace the JSON Reference object with the resolved string value before validating against the JSON Schema? Or are references limited to object types only?

Thanks to anyone who can shed some light onto this.

Était-ce utile?

La solution

I wouldn't expect most validators to resolve JSON References before validation. You could either:

  • resolve JSON References before validation
  • adapt the JSON Schemas to allow for JSON Reference objects in certain places

Personally, I think the first option is much neater.

You could end up with circular references I suppose - I don't know which validator/language you're using, but tv4 can definitely handle it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top