40

json2.js seems to ignore members of the parent object when using JSON.stringify(). Example:

require('./json2.js');

function WorldObject(type) {    
    this.position = 4;
}

function Actor(val) {
    this.someVal = 50;
}

Actor.prototype = new WorldObject();

var a = new Actor(2);

console.log(a.position);
console.log(JSON.stringify(a));

The output is:

4
{"someVal":50}

I would expect this output:

4
{"position":0, "someVal":50}
wtjones
  • 4,090
  • 4
  • 34
  • 41

6 Answers6

42

Well that's just the way it is, JSON.stringify does not preserve any of the not-owned properties of the object. You can have a look at an interesting discussion about other drawbacks and possible workarounds here.

Also note that the author has not only documented the problems, but also written a library called HydrateJS that might help you.

The problem is a little bit deeper than it seems at the first sight. Even if a would really stringify to {"position":0, "someVal":50}, then parsing it later would create an object that has the desired properties, but is neither an instance of Actor, nor has it a prototype link to the WorldObject (after all, the parse method doesn't have this info, so it can't possibly restore it that way).

To preserve the prototype chain, clever tricks are necessary (like those used in HydrateJS). If this is not what you are aiming for, maybe you just need to "flatten" the object before stringifying it. To do that, you could e.g. iterate all the properties of the object, regardless of whether they are own or not and re-assign them (this will ensure they get defined on the object itself instead of just inherited from the prototype).

function flatten(obj) {
    var result = Object.create(obj);
    for(var key in result) {
        result[key] = result[key];
    }
    return result;
}

The way the function is written it doesn't mutate the original object. So using

console.log(JSON.stringify(flatten(a)));

you'll get the output you want and a will stay the same.

Dai
  • 141,631
  • 28
  • 261
  • 374
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
27

Another option would be to define a toJSON method in the object prototype you want to serialize:

function Test(){}

Test.prototype = {

    someProperty: "some value", 

    toJSON: function() {
        var tmp = {};

        for(var key in this) {
            if(typeof this[key] !== 'function')
                tmp[key] = this[key];
        }

        return tmp;
    }
};

var t = new Test;

JSON.stringify(t); // returns "{"someProperty" : "some value"}"

This works since JSON.stringify searches for a toJSON method in the object it receives, before trying the native serialization.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
Cesar Varela
  • 5,004
  • 2
  • 16
  • 17
11

Check this fiddle: http://jsfiddle.net/AEGYG/

You can flat-stringify the object using this function:

function flatStringify(x) {
    for(var i in x) {
        if(!x.hasOwnProperty(i)) {
            // weird as it might seem, this actually does the trick! - adds parent property to self
            x[i] = x[i];
        }
    }
    return JSON.stringify(x);
}
techfoobar
  • 65,616
  • 14
  • 114
  • 135
5

Here is a recursive version of the snippet @TomasVana included in his answer, in case there is inheritance in multiple levels of your object tree:

var flatten = function(obj) {
    if (obj === null) {
        return null;
    }

    if (Array.isArray(obj)) {
        var newObj = [];
        for (var i = 0; i < obj.length; i++) {
            if (typeof obj[i] === 'object') {
                newObj.push(flatten(obj[i]));
            }
            else {
                newObj.push(obj[i]);
            }
        }
        return newObj;
    }

    var result = Object.create(obj);
    for(var key in result) {
        if (typeof result[key] === 'object') {
            result[key] = flatten(result[key]);
        }
        else {
            result[key] = result[key];
        }
    }
    return result;
}

And it keeps arrays as arrays. Call it the same way:

console.log(JSON.stringify(flatten(visualDataViews)));
kiewic
  • 15,852
  • 13
  • 78
  • 101
2

While the flatten approach in general works, the snippets in other answers posted so far don't work for properties that are not modifiable, for example if the prototype has been frozen. To handle this case, you would need to create a new object and assign the properties to this new object. Since you're just stringifying the resulting object, object identity and other JavaScript internals probably don't matter, so it's perfectly fine to return a new object. This approach is also arguably more readable than reassigning an object's properties to itself, since it doesn't look like a no-op:

function flatten(obj) {
    var ret = {};
    for (var i in obj) {
        ret[i] = obj[i];
    }
    return ret;
}
William Luc Ritchie
  • 1,666
  • 1
  • 11
  • 15
1

JSON.stringify takes three options

JSON.stringify(value[, replacer[, space]])

So, make use of the replacer, which is a function, that is called recursively for every key-value-pair.

Next Problem, to get really everything, you need to follow the prototpes and you must use getOwnPropertyNames to get all property names (more than you can catch with keysor for…in):

var getAllPropertyNames = () => {
  const seen = new WeakSet();
  return (obj) => {
    let props = [];
    do {
      if (seen.has(obj)) return [];
      seen.add(obj);
      Object.getOwnPropertyNames(obj).forEach((prop) => {
        if (props.indexOf(prop) === -1) props.push(prop);
      });
    } while ((obj = Object.getPrototypeOf(obj)));
    return props;
  };
};
var flatten = () => {
  const seen = new WeakSet();
  const getPropertyNames = getAllPropertyNames();
  return (key, value) => {
    if (value !== null && typeof value === "object") {
      if (seen.has(value)) return;
      seen.add(value);
      let result = {};
      getPropertyNames(value).forEach((k) => (result[k] = value[k]));
      return result;
    }
    return value;
  };
};

Then flatten the object to JSON:

JSON.stringify(myValue, flatten());

Notes:

  • I had a case where value was null, but typeof value was "object"
  • Circular references must bee detected, so it needs seen
Marc Wäckerlin
  • 662
  • 6
  • 7