Thursday, 27 January 2005

JSON and its implementation gotcha

Stephen points to a slashdot post about JSON (Javascript Object Notation) promoted by Douglas Crockford.

Fablusi replies heavily on JSON for its communication with the server and inter-module communication. However during our implementation, I discover a few issues which are not discussed anywhere else known to me. (Please point me to any discussion of JSON, if you know of any).

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. :-(

Current Fablusi v2 users will notice that the save format of the simulation data is in traditional Javascript object initialisation format - NOT JSON because I need the initialised objects to have methods defined in the system.

While JSON is a good idea and I look forward to seeing how JSON-RPC may work, use with an understanding of its limitation. The benefit (light weight) outweighs its limitation.

2 comments:

Crockford said...
This comment has been removed by a blog administrator.
Crockford said...

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. :-)