2

Here's my class:

export class Patient {
constructor(public id: number, public name: string, public location: string, public bedId: number, public severity: string,
public trajectory: number, public vitalSigns: [GraphData[]], public latestReading: GraphData[]){
}
 public get getCombinedVSData(): Array<GraphData>{
    let combinedVitalSigns: GraphData[] = [];
    for (let data of this.vitalSigns){
        combinedVitalSigns.push(data[0]);
    }
    return combinedVitalSigns;
}

}

When I print one of my patients,

console.log(this.patientService.patients[0]);

I get this:

enter image description here

In my application, I need to convert the patient to JSON to move it with drag and drop:

let thisIsNotFine=JSON.stringify(this.patientService.patients[0]);

Here comes the problem. When I convert it back to a JS object and print it,

console.log(JSON.parse(thisIsNotFine));

I get this:

enter image description here

As you can see, it is no longer called a Patient in the console, and the getCombinedVSData getter is gone.

Is this normal behaviour? How can I keep the getter on the object when converting it to JSON? Thank you.

sauntimo
  • 1,531
  • 1
  • 17
  • 28
Jesper
  • 2,644
  • 4
  • 30
  • 65
  • if you convert back from json string you get instance of Object. to get a instance of Pation use: `let result = (JSON.parse(myJsonString) as Patient);` – mtizziani Jul 21 '17 at 06:30
  • **Voted to reopen this question.** The “JSON stringify ES6 class property with getter/setter” question, which this question marked as a duplicate of, is about excluding specific properties when JSON-stringifying. This is about JSON not preserving functions. How come they are the same questions? – Константин Ван Jul 24 '19 at 08:11

1 Answers1

2

From the JSON docs, JSON can't include functions so your method won't be parsed as JSON. Valid data types are string, number, object, array, true, false and null.

Here's an example of json.stringify() and json.parse() removing methods from an object:

var obj = {
 "prop" : "value",
    "method" : function( num ){
     return num + 1;
    }
};

var json = JSON.stringify( obj );

var new_obj = JSON.parse( json );

Object.keys( new_obj ).forEach(function(key){
 console.log( key + " : " + new_obj[key]  );
});
sauntimo
  • 1,531
  • 1
  • 17
  • 28
  • 1
    You can, however, pass a function as the second argument to `JSON#stringify` to define custom behavior for when a function or other Object is encountered, such as calling the .toString(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter – Khauri Jul 20 '17 at 16:05
  • If this solves your problem you could accept the answer so that people will know this is resolved? – sauntimo Jul 20 '17 at 16:26
  • 1
    It's not a normal method; it's a property getter. Similar problem, but a little different explanation. A good way to do it, besides the second argument to `stringify`, is to use [`toJSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior). – Heretic Monkey Jul 20 '17 at 16:54
  • @MikeMcCaughan, nice, I didn't know that. – sauntimo Jul 20 '17 at 16:56