views:

2129

answers:

3

I know how to use JSON to create objects, but there doesn't seem to be away to use JSON to create an object that is of a specific object type.

Here's an example of an Object and creating an instance of it:

Person = function() { };
Person.prototype = {
    FirstName: null,
    GetFirstName: function() {
        return this.FirstName;
    }
};

//Create an instance of the Person Object
var me = new Person();
me.FirstName = "Chris";
alert(me.GetFirstName()); //alert the FirstName property

Now, I would like to use JSON to create a new Person object so that the GetFirstName function works on it.

Here's something like that I'm looking to do (but this code doesn't work):

var you = new Person() { FirstName: "Mike" };
// OR
var you = new Person{ FirstName: "Mike" };

Is there anyway to use JSON to create an object that is of a specific type?

UPDATE: My sample with the Person object is just to simplify the question. In fact, I am unable to modify the constructors of the actual objects that I need to create instances of. The objects are part of a third-party library.

UPDATE: Using some of the suggestions below, I was able to figure out a way to create an object that inherits from the original, and accept JSON in it's constructor. This is neat!

personWrapper = function(obj){
    for(var o in obj){
        this[o] = obj[o];
    }
};
personWrapper.prototype = new Person();

var you = new personWrapper({FirstName: "Chris"});
alert(you.GetFirstName());
alert(you instanceof Person); // returns True - we are successfully inheriting from Person!
+7  A: 

I don't imagine so. I'd create a function on the Person class to initialise from a JSON object if I were you.

function Person() {
    this.loadFromJSON = function(json) {
        this.FirstName = json.FirstName;
    };
}

If you didn't know what class the JSON object was representing beforehand, perhaps add an extra variable into your JSON.

{ _className : "Person", FirstName : "Mike" }

And then have a 'builder' function which interprets it.

function buildFromJSON(json) {
    var myObj = new json["_className"]();
    myObj.loadFromJSON(json);
    return myObj;
}


Update: since you say the class is part of a third-party library which you can't change, you could either extend the class with prototyping, or write a function which just populates the class externally.

eg:

Person.prototype.loadFromJSON = function(json) {
    // as above...
};

or

function populateObject(obj, json) {
    for (var i in json) {
        // you might want to put in a check here to test
        // that obj actually has an attribute named i
        obj[i] = json[i];
    }
}
nickf
This is pretty much exactly how Microsoft Ajax works. You define a method that returns a specific type. The ajax layer queries that type and creates an js object proxy. Then, all json calls return a '__type' parameter that tells the library which proxy to use to instantiate the object.
Travis
+4  A: 

You could allow new Person() to accept an object to populate attributes with as a parameter.

var you = new Person({ firstName: 'Mike' });
Aupajo
In fact this is how several JS frameworks out there take in an "options" object to override the default properties of an object at construction time.
Ates Goral
+2  A: 

You can derive an object from theirs. Your constructor can accept the object you want, but call their constructor in an unaffected fashion:

function yourWrapper(obj) {
    theirObject.call(this);
    for (var s in obj) {
        this[s] = obj[s];
    }
}
yourWrapper.prototype = new theirObject();

Or something like that :)

harley.333