I think about saving objects using JSON.stringify
and later load them again. The "stringification" works well of course, however I am not sure how I would set the prototype/class of an object.
Asked
Active
Viewed 139 times
1

VisioN
- 143,310
- 32
- 282
- 281

user1680104
- 8,437
- 4
- 22
- 27
-
FYI JSON.stringify is not on all browsers versions..... – Royi Namir Jan 11 '13 at 21:19
-
1Could you provide an example of what you want to achieve? – VisioN Jan 11 '13 at 21:19
-
1@RoyiNamir But it is available as a separate download from http://json.org. – Michael Todd Jan 11 '13 at 21:20
-
@MichaelTodd Did you see the 3 first letters in my comment :-) ? – Royi Namir Jan 11 '13 at 21:20
-
@RoyiNamir Hm... so what? :) – VisioN Jan 11 '13 at 21:23
-
@VisioN Comment to let the OP **to know** there is a trap. – Royi Namir Jan 11 '13 at 21:25
-
@RoyiNamir While Michael's comment just stated the solution for that. Nothing special. – VisioN Jan 11 '13 at 21:26
-
@VisioN there is a difference between _But it is available as a separate..._ VS _And it is available as a separate.._ . The [but] can make other people to think the FYI comment is incorrect. while it is , AND there is a solution which can be found at... – Royi Namir Jan 11 '13 at 21:29
-
Please be sure to check the most helpful answer , so that other people will continue helping you :-) – Royi Namir Jan 12 '13 at 12:40
1 Answers
1
You can use .toJSON
and reviver callback to achieve this.
Here is the scheme:
function Person() {
this.name = "name";
this.age = "age";
}
Person.prototype.toJSON = function() {
//Define this for other classes as well
return {__class__: "Person", name: this.name, age: this.age};
};
function reviver( key, value ) {
if( typeof value == "object" && value.__class__ ) {
var ret = new window[value.__class__];
for( var k in value ) {
if( k === "__class__" ) continue;
ret[k] = value[k];
}
return ret;
}
return value;
}
var a = new Person(),
b = new Person();
var json = JSON.stringify( [a,b] );
var decoded = JSON.parse( json, reviver);
console.log( decoded ); //[Person, Person] I.E. array of Person instances instead of plain objects
in this simplified scheme, the class must be global.

Esailija
- 138,174
- 23
- 272
- 326
-
**brilliant answer**. ( p.s. In real life , should I embed what type of Object the json string was?) – Royi Namir Jan 12 '13 at 07:01
-
Testing your code , IMHO you missed `()` after `new window[value.__class__];`. – Royi Namir Jan 12 '13 at 07:06
-
-
nevermind that why the browser warned me about.... why did you used `new window [...]` ? – Royi Namir Jan 12 '13 at 09:56
-
@RoyiNamir `new window[value.__class__]` == `new window["Person"] == `new Person` – Esailija Jan 12 '13 at 09:57
-
-
@RoyiNamir not really... that is a waste of eval, the bracket syntax works. The eval is evil actually comes from people using eval over bracket syntax – Esailija Jan 12 '13 at 10:05
-
So creating objects with Eval can be totaly replaced by `new window[x]` ? – Royi Namir Jan 12 '13 at 12:39
-
@RoyiNamir no, property access with dynamic strings can... come on I'm sure you know about `obj.prop` vs `obj["prop"]`? – Esailija Jan 12 '13 at 12:46
-
1@RoyiNamir in the second, we can also use a variable.. like `var a = "prop"` and then `obj[a]` – Esailija Jan 12 '13 at 12:50
-
I meant :`var o={}; o["22"]=123; //its ok` ...while this : `o.33=333;` will fail – Royi Namir Jan 12 '13 at 12:58
-
you made me ask this question :-) http://stackoverflow.com/questions/14294714/json-parse-reviver-function-has-n1-keys – Royi Namir Jan 12 '13 at 15:22
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22637/discussion-between-royi-namir-and-esailija) – Royi Namir Jan 12 '13 at 15:39