Skip to content

JSON Deserialization

Vojtěch Habarta edited this page Dec 6, 2017 · 2 revisions

JSON Deserialization extension

typescript-generator can generate TypeScript classes but when you parse JSON you get simple objects that are not instances of any classes.

You can use JSON deserialization extension - 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.

Let's say we have following class User:

export class User {
    name: string;
    childAccount: boolean;
    age: number;
}

then this extension will add (in principal) following fromData method:

export class User {
    // ...
    static fromData(data: User): User {
        const instance = new User();
        instance.name = data.name;
        instance.childAccount = data.childAccount;
        instance.age = data.age;
        return instance;
    }
}

This is simplified sample, in fact it also tests data for null and undefined and it also handles inheritance.

This 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.

Usage

Configuration of this extension in little bit verbose in Maven but you can just copy relevant part from following snippets to your pom.xml or adapt it to your build system.

<configuration>
    <outputFileType>implementationFile</outputFileType>
    <outputKind>module</outputKind>
    <mapClasses>asClasses</mapClasses>
    <extensions>
        <extension>cz.habarta.typescript.generator.ext.AxiosClientExtension</extension>
        <extension>cz.habarta.typescript.generator.ext.JsonDeserializationExtension</extension>
    </extensions>
    ...
</configuration>

If you are also generating REST application client you can let this extension to deserialize data from HTTP response. This can be turned on using useJsonDeserializationInJaxrsApplicationClient parameter.

<configuration>
    <outputFileType>implementationFile</outputFileType>
    <outputKind>module</outputKind>
    <mapClasses>asClasses</mapClasses>
    <extensionsWithConfiguration>
        <extension>
            <className>cz.habarta.typescript.generator.ext.AxiosClientExtension</className>
        </extension>
        <extension>
            <className>cz.habarta.typescript.generator.ext.JsonDeserializationExtension</className>
            <configuration>
                <useJsonDeserializationInJaxrsApplicationClient>true</useJsonDeserializationInJaxrsApplicationClient>
            </configuration>
        </extension>
    </extensionsWithConfiguration>
    ...
</configuration>

Adding methods to generated classes

If you have generated classes and JSON data deserialized into instances of these classes you can add your custom methods to these classes. Here is an example:

// augment User class
declare module "./generated-user" {
    interface User {
        equals(other: User): boolean;
    }
}
User.prototype.equals = function (this: User, other) {
    return this.name === other.name;
};

// use augmented class
const user1 = User.fromData({ name: "typescripter" });
const user2 = User.fromData({ name: "typescripter" });
assertEquals(user1.equals(user2), true);
Clone this wiki locally