0

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.

Community
  • 1
  • 1
sumitb.mdi
  • 1,010
  • 14
  • 17
  • 6
    I think you're confused about how TypeScript works. It's just a compile-time typing system (and transpiler). No typing information is present, or enforced, at run time. You'll have to ensure that the JSON coming down has the right shape yourself, or use some kind of schema-based validation library, or find a smart serialization/deserialization library. –  Mar 25 '17 at 17:23
  • @torazaburo Thanks a lot. Your information intrigued me to do some research on the TypeScript, and I found that I was expecting something from TypeScript which it wasn't made for. Constructs like TypeScript Interfaces are not even present in the Javascript at run time. So I am finally using TypedJSON (https://github.com/JohnWeisz/TypedJSON) library for Serialization/Deserialization so that the behaviour at runtime is as I was expecting. – sumitb.mdi Mar 26 '17 at 11:16
  • Why not just cast the JSON object?: let person: Person = JSON.parse(jsonString)) – yoonjesung May 08 '17 at 13:34
  • @yoonjesung Yes, you can do that, but the problem will be that let's say you have some extra property in your jsonString which is not present in your "Person" interface, at run-time you will assume that you have an object of type "Person", but once you serialize this object, it will contain those extra properties. So not purely what you will expect from serializing the object of type "Person". – sumitb.mdi May 10 '17 at 12:25

2 Answers2

0

If you use JSON.Parse from JSON to Person object, the prototype of the Person object is not created. Which means, if you've added a getter on the Person object, for instance, you cannot access it, because it is not available on the JSON. What you need to do create a new Person Object and map each of the properties in your JSON.

There are libraries for serializing/deserializing of JSON to Object (Person class). Checkout below library for your reference.

https://www.npmjs.com/package/@peerlancers/json-serialization

Yhan
  • 51
  • 5
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](https://stackoverflow.com/help/deleted-answers). – Alessio Oct 08 '19 at 06:57
  • Thanks for the hint @Alessio, really appreciate it. I've already updated my answer. – Yhan Oct 09 '19 at 08:15
0

The other answers are correct. You should consider using 3-d party libraries for proper typescript class-json serialzation such as https://www.npmjs.com/package/ts-jackson

Elias
  • 698
  • 8
  • 23