__propertyTypes__
property on the class prototype. For example:Object
instance using JSON.parse
-- after that, we can iterate over the entires in __propertyTypes__
(collected above) and instantiate the required properties accordingly. The type of the root object must be specified, so that the deserializer has a starting-point.Person
is expected, then it is a Person
instance that is created. With some additional security measures in place for primitive types and arrays, this approach can be made secure, that resists any malicious JSON.Object.assign
I don't know when this was added, I'm currently using Typescript 2.0.2, and this appears to be an ES6 feature.HalJson
JSON.parse
: ts.quicktype.ioJSON.parse
against the type map:cz.habarta.typescript.generator.ext.JsonDeserializationExtension
- which generates methods that allow to 'deserialize' JSON data into instances of generated classes.It adds 'copy' methods to classes which receive pure object, create new instance of the class and recursivelly copy properties from data object to the instance of class.User
:fromData
method:null
and undefined
and it also handles inheritance.User
example contains only properties of simple types but typescript-generator copy all constructs that it generates like arrays, objects, discriminated union types, generics etc.pom.xml
or adapt it to your build system.useJsonDeserializationInJaxrsApplicationClient
parameter.