Thursday, October 11th, 2007

JSON News: JSON Schema and JSON Referencing

Category: JSON

Kris Zyp has been plugging away on a couple of interesting JSON topics:

JSON Referencing Schemes

There has been a lot of discussion about handling referencing schemes, so Kris has gotten the various ideas and summarized them.

He takes the following object call:

javascript

  1. obj = {name:"foo", child: {"name" : "bar"}};
  2. obj.child.parent = obj;

and shows:

javascript

  1. // fixups scheme
  2.  
  3. {"result":{"name":"foo", "child": {"name" : "bar"}},
  4.   "fixups":[[["child","parent"],[]]]}
  5.  
  6. // id referencing
  7.  
  8. {"name":"foo", "id":"1", "child": {"name" : "bar","parent":{"id":"1"}}}
  9.  
  10. //or
  11.  
  12. {"name":"foo", "$id":"1", "child": {"name" : "bar","parent":{"$idref":"1"}}}
  13.  
  14. // path referencing
  15.  
  16. {"name":"foo", "child": {"name" : "bar","parent":{"id":"$"}}}
  17.  
  18. // or
  19.  
  20. {"name":"foo", "child": {"name" : "bar","parent":"$jref:this"}}

JSON Schema Proposal

Kris has proposed a schema definition which would look a bit like this:

javascript

  1. {
  2.   "name": {"type":"string",
  3.     "required":true},
  4.   "age" : {"type":"number",
  5.     "maximum":125}
  6. }

There are some questions and thoughts about it and he is looking for community feedback on all of this stuff!

Posted by Dion Almaer at 10:01 am
7 Comments

+++--
3.6 rating from 22 votes

7 Comments »

Comments feed TrackBack URI

The schema work looks really interesting, and something I might look at using over at microjson.org.

Seems to keep things nice and simple.

Comment by JP — October 11, 2007

You could do something using inline comments, applying a ref-counter just like the PHP-way…
Could look something like this:

var obj = {
“name”:”foo”,
“child”: {
“name” : “bar”,
“parent”: /*r:1*/ null,
“brother”: {
“name” : “baz”,
“parent”: /*r:1*/ null,
“brother” : /*r:3*/ null
}
},
“anotherChild” : /*r:6*/ null
};

The /*r:n*/ meaning a reference to the nTh thing serialized, leaving null as default value. It keeps a valid Javascript relying on comments to keep the references…

Comment by Pablo Cabrera — October 11, 2007

Besides being cryptic, it is not valid JSON. This isn’t JavaScript, it is JSON which does not have a notion of comments.

Comment by Kris Zyp — October 11, 2007

The fixups scheme was originally designed to be ultra simple and intuitive, in that they would simply be a list of JS assignment statements.

It was switched to arrays of reference “paths” because it’s much easier to implement a parsing algorithm in any language.

For the back story and reasoning behind the “fixups” method, look here:

http://code.google.com/p/jabsorb/issues/detail?id=6

and here:

http://groups.google.com/group/jabsorb-user/browse_thread/thread/36fd6575913f2c06

and of course, here:

http://www.json.com/2007/10/09/json-referencing-schemes/

We have this all working great in the upcoming jabsorb 1.2 release that is about to come out… but we are still would welcome suggestions on how to improve it!

Comment by Arthur Blake — October 11, 2007

I generally dislike the notion of consuming the expressive domain of JSON (domain of valid keys for expressing semantic information about Objects) in order to implicitly express details of the structure or syntax. I believe we are faced here with a choice between squeezing names out of the domain while preserving our existing parsers and formatters, or augmenting our parsers and formatters to support additional syntax without universally sacrificing the keys “parent” and “fixup”.

I’m exploring the use of “ref” objects that would accept a subset of JavaScript expressions to create dupicates and cyclic references in a JSON document. Much like JavaScript is a subset of JavaScript’s inline declaration syntax, the strings passed to “ref” would be a subset of JavaScript’s syntax for dereferencing objects through only member selection and item indexing. It would looks something like:

data = {‘A’: {‘B’: [[ref(‘A.B[0]’)]]}}
such that:
data[‘A’][‘B’][0] == data[‘A’][‘B’][0][0]

I’ve posted a proof-of-concept for the client-side reference resolution and parsing:

https://cixar.com/tracs/javascript/browser/trunk/cson.js

https://cixar.com/tracs/javascript/browser/trunk/test/cson.js

Comment by Kris Kowal — October 12, 2007

The fixup scheme was designed as a simple way to pass circular references and / or duplicate references over json-rpc.

It doesn’t consume anything in the json domain (as opposed to embedded id’s) which is one reason we like it– it purposefully keeps the fixups entirely outside of the json domain, and is passed as a separate structure alongside the json.

Comment by Arthur Blake — October 12, 2007

The most critical aspects of a common referencing convention in the open web is readability and flexibilty, consuming domain is relatively small issue (we can prefix names with $, as ES suggests this for generating names, so it would be implied for JS, if desired). This has been the major driver behind most standards(otherwise the web would be a bunch of binary formats). Fixups is neither readable or flexible. Kris, your scheme is an interesting idea, highly readable, and could be very flexible, although it is not valid JSON. Amongst the referencing schemes, the techniques are not all that different, I am still hoping we can settle on one convention, even it’s isn’t everyone’s favorite. As mashups grow more popular, and more complex data is passed, a common convention going to become increasingly important. I am hoping we can move past all of us just do our own thing and continuing to diverge…

Comment by Kris Zyp — October 12, 2007

Leave a comment

You must be logged in to post a comment.