Wednesday 16 February 2005

JSON and its implementation gotcha

Douglas Crockford stopped by and leave a comment to my post of JSON and its implementation gotcha. Woo Hoo!

That's what I wrote:

1. Internet Explorer generally fails to initialise an object in JSON notation. However, it loads an array of objects correctly. So if you want to initialise a JSON object in cross browser application, use this pattern:
thisObj=eval("[{'membersName':'MemberValue',...}]")[0];
i.e. transfer using an array instead of an object, or add the square brackets at the client side before conversion.

2. There are other even lighter weight mechanism for transferring a set of data of the same object. (by not repeating the membersName many times..., but require some software support at the receiving end.)

3. the evaluated object does not have any methods. It is data only. This can proved to be quite difficult to use the object effectively.

4. Eval is a depreciated Javascript method. :-(


and this is his comment:

1. I have not found this to be a problem.

2. By light-weight, we are talking about programmer efficiency, not payload size.

3. This is not a problem. Think of a JSON object as a sort of hash table. Uncoupling from a brittle class structure can be more efficient in many applications, particularly in class-free languages like JavaScript.

4. The object.eval() method is deprecated, but the global eval function is not. :-)


By the way, his JavaScript Lint "is a JavaScript program that looks for problems in JavaScript programs." I used it together with HTMLKit and have healed a lot of my headaches. Instruction to install of JS Lint with HTMLKit can be found here.

No comments: