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