dermoritz
dermoritz

Reputation: 13001

Deserialize JSON into specific/existing JavaScript object

Is there a way to parse a JSON string into an existing Javascript object: Lets say i have created this object:

var ClientState = function(){
    this.userId ="";
    this.telephoneState = "UNKNOWN";
    this.agentState = "UNKNOWN";
    this.muteState = "UNKNOWN";
    this.number = "";
    this.ready = false;
}

ClientState.prototype = {
    doNastyStuff: function(){
        //do something here
    }
    //other methods here
}

I have this json coming through the wire:

{"userId":"xyz","telephoneState":"READY","agentState":"UNKNOWN","muteState":"MUTED","number":"","ready":false}

Is it possible to deserialize into the object specified above? So that i can use all methods specified on it? Or in general is it possible to deserialize into a specific target object (without specifying deserialization in this target object)? (I know that i could create an constructor that accepts json or a parsed object.)

Upvotes: 1

Views: 1597

Answers (3)

Andy
Andy

Reputation: 63524

If you want to use your function to add the object values to the instance properties:

function ClientState() {
  this.userId = "";
  this.telephoneState = "UNKNOWN";
  this.agentState = "UNKNOWN";
  this.muteState = "UNKNOWN";
  this.number = "";
  this.ready = false;
}

ClientState.prototype = {
  doNastyStuff: function(json) {
    const data = JSON.parse(json);
    Object.assign(this, data);
  }
}

const json = '{"userId":"xyz","telephoneState":"READY","agentState":"UNKNOWN","muteState":"MUTED","number":"","ready":false}';
const clientState = new ClientState();
clientState.doNastyStuff(json);
console.log(clientState);

Upvotes: 0

Máté Safranka
Máté Safranka

Reputation: 4106

Is this what you had in mind?

function parseAs(targetClass, rawJSON) {
    return Object.assign(new targetClass(), JSON.parse(rawJSON));
}

var clientState = parseAs(ClientState, '{"userId":"xyz"}');

I don't think there's a native JSON method that does this, but you can just write a function like the one above. You can even define it on the JSON class itself:

JSON.parseAs = function(targetClass, rawJSON) {
    return Object.assign(new targetClass(), JSON.parse(rawJSON));
}

Upvotes: 0

31piy
31piy

Reputation: 23859

Yes! You can use Object.assign to overwrite the attributes of an object with another object:

var ClientState = function() {
  this.userId = "";
  this.telephoneState = "UNKNOWN";
  this.agentState = "UNKNOWN";
  this.muteState = "UNKNOWN";
  this.number = "";
  this.ready = false;
}

var c = new ClientState();
console.log('prior assignment: ', c);

Object.assign(c, {
  "userId": "xyz",
  "telephoneState": "READY",
  "agentState": "UNKNOWN",
  "muteState": "MUTED",
  "number": "",
  "ready": false
});

console.log('after assignment: ', c);

Note that it will overwrite all the properties of source object (first object) with the target object (second object) by matching the respective keys. The keys, which are non-existing in the target object are left intact in the source object.

Upvotes: 2

Related Questions