0

I want to Encode EXTjs Class to Json, But, I can't..

I use JSON.stringify, but that gives Exception with Type error.

How can I do that?

Thanks and Here my Code.

Ext.define('Text',{
 extend : 'Ext.Img',
 x : 50,
 y : 50,
 size : 100,
 text : 'Text',
 name : 'Text',
 src : ' ',
 tag : '',
 Events : []
});
var text = new Text();
var temp = JSON.stringify(text);
Saket Patel
6,7031 gold badge29 silver badges36 bronze badges
asked Apr 13, 2012 at 7:44

2 Answers 2

8

try using

Ext.encode(Object)

it Encodes an Object, Array or other value & returns The JSON string.

refer Ext.JSON

serialize object

answered Apr 13, 2012 at 8:00
Sign up to request clarification or add additional context in comments.

3 Comments

I tried, but Error.. 'RangeError: Maximum call stack size exceeded'
try var seen = []; var temp = JSON.stringify(text, function(key, val) { if (typeof val == "object") { if (seen.indexOf(val) >= 0) return undefined seen.push(val) } return val }); console.log(temp);
But, I solve this problem, but it is not smart.. I made parser for Class. That error's cause is the Ext core is include in Class. So I made Parser. I get Idea from your answer. Thanks! If you know other way, Please tell me.
5

The problem here is that ExtJS creates internal references on the objects, which turn out to be cyclical. Thus, the default JSON serializer fails.

You need to manually define a toJSON method which will be called by JSON.stringify:

Ext.define('Text', {
 extend : 'Ext.Img',
 x : 50,
 y : 50,
 size : 100,
 text : 'Text',
 name : 'Text',
 src : ' ',
 tag : '',
 Events : [],
 toJSON: function () {
 return 'Whatever you like' + this.text + this.size // etc.
 }
});
JSON.stringify(new Text()); // "Whatever you likeText100"
answered Apr 17, 2012 at 20:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.