I just want to deserialise the JSON I am getting it from my server API.
For most of the solutions mentioned out there (without using any third party library), it looks like the problem is solved. For example, the most up-voted answer on this SO thread : HERE
But the caveat which I observe is that the behaviour is not what I am expecting, when there is some extra property in json response which I am not expecting.
Let's take an example :
class Person {
id: number;
name: string;
}
let jsonString = `{
"id": 1,
"name": "sumit",
"age": 23
}`;
let person: Person = Object.assign(new Person(), JSON.parse(jsonString));
console.log(Object.keys(person));
console.log(JSON.stringify(person));
So when I work with "person" object later on, I expect that it contains only those properties which are mentioned in Person class, after all "person" is of type "Person". But surprisingly on serializing person, it contains those extra properties which are not in Person class. Here "age"
Output for the above is :
["id", "name", "age"]
{"id":1,"name":"sumit","age":23}
Now, how do I ensure that after the Deserialization, object "person" is not having any extra properties.