Convenience and standards
The JSON specification is pretty simple and one thing I really want to add is proper support for JavaScript dates.
E.g. instead of outputting this:
{"posted": "Fri, 30 May 2008 18:11:02 GMT"}
we output this:
{"posted": new Date("Fri, 30 May 2008 18:11:02 GMT")}
This isn't JSON thought any longer, since the output would be JavaScript and not JSON. The pro of this is that it's really convenient to use with JavaScript, the bad thing about this is that it breaks JSON parsing. If you don't have this support, you must manually convert dates to JavaScript date object and sometimes this isn't so straightforward, an example: if(isDict(offset)) {
map(keys(offset), function(k) {
if(offset[k])
offset[k] = new Date(offset[k]);
});
this.offset = offset;
}
else {
this.offset = new Date(offset);
}
If you support this convience, then the above code is a one liner: this.offset = offset;
I have chosen the convenience to keep the JavaScript code as simple as possible and down the road provide an API that properly supports JSON.
27. Oct 2008
•
Code
·
Code rewrite
·
Design
|
|