This project is archived and is in readonly mode.

#955 ✓invalid
jackvangalen (at hotmail)

JSON.encode instance members not encoded

Reported by jackvangalen (at hotmail) | August 3rd, 2010 @ 02:57 PM

See the little snippet below (ready to be copy-paste into firebug):

var MyClass = new Class({ 
        settings: {
        'a' : 'aa',
        'b' : 'bb'
    getSettings1: function(){
        return this.settings;
    getSettings2: function(){
        var result = {};
        $H(this.settings).each(function(item, key){
            result[key] = item;
        return result;

settings1 = (new MyClass()).getSettings1();
settings2 = (new MyClass()).getSettings2();
settings3 = {'a' : 'aa', 'b' : 'bb'};

console.log(JSON.encode(settings1));    //returns {}
console.log(JSON.encode(settings2));    //returns {"a":"aa","b":"bb"}
console.log(JSON.encode(settings3));    //returns {"a":"aa","b":"bb"}

Comments and changes to this ticket

  • Michael Ficarra

    Michael Ficarra August 3rd, 2010 @ 05:05 PM

    • Tag changed from json json.encode to class, instance, object

    This has nothing to do with JSON.encode. Try removing the JSON.encode calls. Same result.

    It is odd, though. It looks like it should work. Returning single members of the settings object in getSettings1 works as expected, but it returns an empty object when attempting to return the entire settings object.

  • jackvangalen (at hotmail)

    jackvangalen (at hotmail) August 3rd, 2010 @ 05:53 PM

    When not using JSON.encode, firebug returns the correct result 3 times. However, when performing the following in Chrome, the internal structure of the objects become clear:

    console.log(settings1);    //returns {}
    console.log(settings2);    //returns {"a":"aa","b":"bb"}
    console.log(settings3);    //returns {"a":"aa","b":"bb"}
    Settings 1: (wrong)
        __proto__: Object    <----????
            a: "aa"
            b: "bb"
            __proto__: Object
    Settings 2 & 3: (correct)
        a: "aa"
        b: "bb"
        __proto__: Object
  • Sebastian Markbåge

    Sebastian Markbåge August 4th, 2010 @ 10:55 AM

    In MooTools, instance objects aren't duplicated for each instance. Instead they're inherited from the original prototype. There are some memory usage benefits to this.

    That's why you're seeing this object structure. JSON.encode only encodes an object's "own" properties. The settings object doesn't have any own properties yet. They're inherited from the original prototype.

    However, if you'd change a property on the instance (this.settings.a = 'cc';) then you'd get {"a":"cc"}. Then you can restore that same settings in a new instance by merging it with the base options.

    var instance1 = new MyClass();
    var settings1 = instance1.settings;
    settings1.a = 'cc';
    var json = JSON.encode(settings1);
    var instance2 = new MyClass();
    var settings2 = instance2.settings;
    Object.append(settings2, JSON.decode(json));
    settings2.a; // 'cc'
    settings2.b; // 'bb'

    The benefit of this is that if you change the default, all saved settings will automatically get the new default.

  • Christoph Pojer

    Christoph Pojer September 3rd, 2010 @ 11:21 AM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins