PDA

View Full Version : scope used for Ext.decode (Ext.util.JSON.decode)



conorarmstrong
1 Jun 2009, 12:00 PM
I've a JSON string needing decoded - say for example



this.b=100;
var json='{a: this.b}';
var obj=Ext.decode(json);
console.log(obj.a); // undefined


The usual Ext.decode() function doesn't inherit the scope of the caller and doesn't allow passing a variable which allows the scope to be specified when decoding the JSON string - eg Ext.decode(json, this);

I've been able to use the javascript call method to execute the Ext.decode() within the scope of this as follows:



this.b=100;
var json='{a: this.b}';
var obj=Ext.decode.call(this,json);
console.log(obj.a); // works


I've had a look at the underlying code of Ext.decode() If the native JSON.parse is available it uses that, otherwise it uses eval() to do the parsing. In my case (Firefox 3.0.10 eval is used)

Does anyone know what scope JSON.parse uses by default if native support exists for it? Would a call method be needed to specify a particular scope?

Also, would it be an idea to modify the implementation of Ext.decode() to allow the passing of scope?

Animal
1 Jun 2009, 12:05 PM
JSON does not have a scope. It is just on object literal containing defined properties. What you have there is not valid JSON.

http://json.org/

conorarmstrong
1 Jun 2009, 12:29 PM
Thanks, that explains a lot!

:)