Jump to content

JSON to TypeScript class instance?


Klaus
 Share

Recommended Posts

Hey there,

 

I've done quite some research, but I'm not totally satisfied with what I found. Just to be sure here's my question: What is actually the most robust and elegant automated solution for deserializing JSON to TypeScript runtime class instances?

Say I got this class:

class Foo {  name: string;  GetName(): string { return this.name };}

And say I got this JSON string for deserialization:

{"name": "John Doe"}

What's the best and most maintainable solution for getting an instance of a Foo class with the name set to "John Doe" and the method GetName() to work? I'm asking very specifically because I know it's easy to deserialize to a pure data-object. I'm wondering if it's possible to get a class instance with working methods, without having to do any manual parsing or any manual data copying. If a fully automated solution isn't possible, what's the next best solution?

 

Thanks and cheers.

 

Link to comment
Share on other sites

  • 2 weeks later...

Quick update in case anybody stumbles upon this thread. I found a solution to my problem. Basically, the following StackOverflow threads helped me achieve my goal:

 

http://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with-a-json-object

http://stackoverflow.com/questions/29758765/json-to-typescript-class-instance (This one is created by me and holds the same question as this thread)

 

Cheers

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...