LostCode
LostCode

Reputation: 533

How to Encode from Extjs.Class to JSON String?

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);

Upvotes: 0

Views: 17041

Answers (2)

user123444555621
user123444555621

Reputation: 152986

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"

Upvotes: 5

MMT
MMT

Reputation: 2216

try using

Ext.encode(Object)

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

refer Ext.JSON

serialize object

Upvotes: 8

Related Questions