JSONic Thoughts

by Kurt Cagle

I've been giving a lot of thought lately to JSON and JavaScript in particular. For those in the XML community not familiar with it, JSON was largely the invention of Douglas Crockford, a JavaScript expert who currently works as an architect for Yahoo! and who I had the privilege of meeting at the recent AJAXWorld conference.

The idea behind JSON is surprisingly simple, and like many simple ideas, is also remarkably powerful. Dissatisfied with the complexity involved with using XML as a data format for seemingly lightweight tasks, Crockford asked one of those questions that causes all kinds of interesting repercussions: "Why couldn't we use the "native" data format of the JavaScript, the associative array, as a vehicle for transferring content between client and server, instead of XML?"

6 Comments

John O'Shea
2006-10-30 13:19:01
Have to admit, that's the first write-up of JSON that hasn't had me dozing - nice one. One small point: the markup at the end of the serializer definition appears to be broken - it ends mid-'for'-loop.
Jörn Horstmann
2006-10-30 14:15:12
JSON is actually a very strictly defined format (see RFC 4627 at http://www.ietf.org/rfc/rfc4627.txt) and there are parsers in many different programming languages. If you extend this format by also serializing javascript functions you are no longer interoperable with non-javascript implementations and loose all benefits of a specified data-interchange format.
Patrick Gosetti-Murrayjohn
2006-10-31 06:10:09
Many thanks! I've been wondering about the virtues of XML over JSON, and this helps tremendously. I'm looking forward to Firefox AJAX Programming!
Kurt Cagle
2006-10-31 10:05:13
John,


Thanks for catching that ... I had a less than sign in my code, and it truncated everything else as if it was an XML tag.


-- Kurt

Kurt Cagle
2006-10-31 10:11:01
Jörn,


You are correct ... and it was a consideration when I was writing this. I'm currently working on a bound application within a fairly strong security context, so that advantage of having the functional notation serialized is pretty strong.


You can make a somewhat weaker version of this without the function serialization by replacing the toJSON() function with the following:


Object.prototype.toJSON = function(){
var recurseObject= function(arg,level){
if (level == null){level =0;}
switch(getType(arg)){
case "string":{
return "\""+arg+"\"";
break;
}
case "number":{
return arg;
break;
}
case "Array":{
var buf = [];
for (var index=0;index!=arg.length;index++){
buf.push(recurseObject(arg[index],level+1));
}
return "["+"n"+'t'.repeat(level)+buf.join(',n'+'t'.repeat(level))+"]";
break;
}
case "Object":{
if (arg == null){
return "null";
}
else {
var buf = [];
for (key in arg){
if (key != "toJSON"){
if (getType(arg[key]) != "function"){
buf.push('"'+key+'":'+recurseObject(arg[key],level+1));
}
}
}
return "{"+"n"+'t'.repeat(level)+buf.join(',n'+'t'.repeat(level))+"}";
}
break;
}
case "function":{
return arg.toString();
break;
}
}
}
return recurseObject(this);
}

Fred
2007-02-01 02:24:40
Just a comment to mention you sample code misses some backslash characters in the regular expression, and for newlines and tabs.