0

Possible Duplicate:
Parse JSON String into a Particular Object Prototype in JavaScript

So I have a class defined like

function ThisClass (a,b,c) {
    this.a = a;
    this.b = b;
    this.c = c;
}
ThisClass.prototype.a_method = function(param) {
    //stuff
}


var instance = new MyClass(a,b,c);
var stringified = JSON.stringify(instance);
var parsed = JSON.parse(stringified);

When I stringify this and store it in a database it's all cool. However, when I parse it after retrieval, it loses its class type, so when viewing it after parsing in chrome's inspect tool, the object is shown as just a regular object, like instance: Object instead of instance: MyClass.

Because of this, the newly parsed variable parsed does not have any of the prototype methods associated with it at instantiation. Any know how to do this, or if I'm doing it wrong?

Thanks!

Community
  • 1
  • 1
Tevis
  • 729
  • 1
  • 12
  • 27

1 Answers1

-1

You can not, JSON is only meant to be used to serialize non circular data object variable.

This the spec for more détails.

3on
  • 6,291
  • 3
  • 26
  • 22
  • There is no reason to suspect there is a circular reference issue. (There are other things that are true in this context, that not being a good one.) –  Sep 17 '12 at 05:01
  • I've never said they where, I just tried to make a useful statement. The second thing to know after you cant serialize functions is about the circular references. I was just trying to be helpful. Thanks for the down vote. I hope it help you feeling better about yourself. Because it does make you look like a jerk. – 3on Sep 17 '12 at 05:08
  • What reason is there for *me* to down-vote this post? (I didn't. So, who "looks like the jerk" now? ;) –  Sep 17 '12 at 05:14