2

My use case is this: I have a deep object structure that I want to serialize and deserialize to/from JSON.

For instance, something like this:

export class A {
    b: B
}

export class B {
    c: C
}

export class C {
    ...
}

let a: A;

(In reality things are even more complicated - some of my types are generics.)

My specific problem is with deserialization - type information is not restored, i.e. members appear as of type Object.

Ideally, I would like a simple statement like this to work:

let a: A = <A>JSON.parse("{b:{c:{...}}}");

Unfortunately, a ends up as of type Object despite of its explicit declaration of type A and despite of the explicit type cast of the parsed value to type A.

A shallow property copy doesn't work either because the members remain of type Object.

The only way that I can think of how to make this work is to bake some code in each type how to deserialize itself from JSON which would be too much work.

My question is: is there a generic way that can restore such a deep object structure from JSON?

This looks like a fairly generic use case. That's why I expect it to be supported natively.

Thanks,
Zlatko

  • 1
    Possible duplicate of [How to do runtime type casting in TypeScript?](http://stackoverflow.com/questions/32167593/how-to-do-runtime-type-casting-in-typescript) -- Also: [JSON to TypeScript class instance?](http://stackoverflow.com/questions/29758765/json-to-typescript-class-instance/29759472) – David Sherret Apr 25 '16 at 18:22
  • I am actually halfway done working on something to solve this problem very easily. I'm going to update the referenced answers once I finish. – David Sherret Apr 25 '16 at 18:29
  • @DavidSherret Mind if I rob a few minutes of your life with a [chat session](http://chat.stackoverflow.com/rooms/110181/typedjson)? I've been working on (and recently, _using_ a now working version of) a solution to this exact same problem: deserializing JSON into actual class instances. – John Weisz Apr 25 '16 at 18:57
  • The task itself is certainly possible and automatable -- however, no released tools exist for it at the moment. At least none of which I know about. I've been working on a [draft](https://github.com/JohnWhiteTB/TypedJSON) to something which might be considered as a generalized solution for this task in the near-future, but it's still a WIP. If you are using a module loading system and up for some adventure, feel free to go ahead and try it. However, I would certainly not recommend it for production use without extensive testing on your end first. – John Weisz Apr 25 '16 at 19:33
  • @DavidSherret: Thanks! I'll take your solution. – Zlatko Michailov - MSFT Apr 25 '16 at 19:54
  • @JohnWhite: I like your syntax, but I don't want to bring in so much utility code. If it's possible to combine the 2 solutions, e.g. decorating fields to avoid David's separate structure, it would be very nice. – Zlatko Michailov - MSFT Apr 25 '16 at 19:57
  • @Zlatko Actually, *decorating fields* is all the linked draft is about. Except for some required cleanup, there is nothing more in there than decorators, the underlying metadata structure, and the serializer/deserializer engine to process this metadata structure. There are no external dependencies. – John Weisz Apr 25 '16 at 20:10

0 Answers0