View Full Version : Support for custom JSON encoding

3 Feb 2010, 8:51 AM
I have had a good look at the following thread:

Obviously, this is a much desired feature. However, the approach presented in the above thread works only if the outer-most object on which Ext.encode is called implements toJSON method. If the object has inner properties, which themselves are objects, and do implement toJSON, this method is not called on them by the private doEncode method of the Ext.util.JSON class.

In my scenario, it is necessary to have custom Json serialization. Is there any intuitive way to get the toJson method to work on an object, without either copy/paste/modify the complete Ext.util.JSON class, or hacking into Ext code??? :s

3 Feb 2010, 11:07 AM
It was frustrating to to hack into the core, and add the following 2 lines in JSON.js at line no. 47.

} else if (o && typeof o.toJSON == 'function') {
return o.toJSON();

However, while studying the JSON class' doEncode method, I had another idea.

Shouldn't we exchange the if statement & for loop at line nos. 49 & 51?
I mean, if the if statement is true on first iteration of the loop, it would remain true on successive iterations. Why then to check a redundant if in each iteration??