Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stringify inherited objects to JSON?

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} 
like image 451
wtjones Avatar asked Jan 08 '12 16:01

wtjones


People also ask

Can you JSON Stringify an object?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Can you JSON Stringify an array of objects?

The JSON. stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified.

Does JSON Stringify nested objects?

stringify does not stringify nested arrays. Bookmark this question. Show activity on this post.

What is JSON Stringify () method?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.


2 Answers

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.

like image 91
Tomas Vana Avatar answered Sep 30 '22 20:09

Tomas Vana


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.

like image 42
Cesar Varela Avatar answered Sep 30 '22 18:09

Cesar Varela