Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Serializer

The main class of the serializer, used to deserialize Objects into class instances in order to add class's prototype to the object.

Simple example:

class Bar {
    prop: string;
    getProp() {
        return this.prop;
    }
}
const serializer = new Serializer();
const bar = serializer.deserialize({ prop: 'foo' }, Bar);
console.log(bar.getProp());
// This will print 'foo' to the console because bar is an instance of Bar,
// not a simple Object anymore.

This implementation can use a Registry to handle inheritance.

Example with inheritance:

@Parent({
     discriminatorField: 'type'
})
class Bar {
    echo() { return 'I am Bar'; }
}

class SubBar extends Bar {
    echo() { return 'I am Sub Bar'; }
}

const serializer = new Serializer();
serializer.registry.add([
     { parent: Bar, children: { 'sub': SubBar }},
]);
const bar = serializer.deserialize({ type: 'sub' }, Bar);
console.log(bar.echo());
// This will print 'I am Sub Bar' to the console
// because bar is an instance of SubBar after following the inheritance.

Hierarchy

  • Serializer

Index

Constructors

constructor

Properties

registry

registry: Registry

Methods

deserialize

  • deserialize<T>(obj: any, clazz: Class<T>, additionalData?: any): T
  • deserialize<T>(array: any[], clazz: [Class<T>], additionalData?: any): T[]
  • Deserialize an object into a specified class.

    For an array of objects, the type specified should be an array of the class.

    Example

    serializer.deserialize({ prop: 'bar' }, Foo); // -> Foo
    serializer.deserialize([{ prop: 'bar' }, {prop: 'baz'}], [Foo]); // -> [Foo, Foo]
    

    Type parameters

    • T

    Parameters

    • obj: any
    • clazz: Class<T>
    • Optional additionalData: any

    Returns T

    an instance of the class T.

  • Deserialize an array of objects into an array of specified classes.

    For an array of objects, the type specified should be an array of the class.

    Example

    serializer.deserialize({ prop: 'bar' }, Foo); // -> Foo
    serializer.deserialize([{ prop: 'bar' }, {prop: 'baz'}], [Foo]); // -> [Foo, Foo]
    

    Type parameters

    • T

    Parameters

    • array: any[]
    • clazz: [Class<T>]
    • Optional additionalData: any

    Returns T[]

    an array of instances of the class T.

Protected deserializeArray

  • deserializeArray<T>(array: any[], clazz: Class<T>, additionalData?: any): T[]
  • Deserialize an array of objects into an array of specified classes.

    Type parameters

    • T

    Parameters

    • array: any[]

      The array of objects.

    • clazz: Class<T>

      The class constructor.

    • Optional additionalData: any

      additional data you want to add to keep trace of a context during serialization in a child class, the goal behind that is to provide a way for serializers extending this one to use custom data across a single object, no matter how deep we are in the object.

    Returns T[]

    An array of instances of the type T.

Protected deserializeObject

  • deserializeObject<T>(obj: any, clazz: Class<T>, additionalData?: any): T
  • Deserialize an object into a specified class.

    Type parameters

    • T

    Parameters

    • obj: any

      The object.

    • clazz: Class<T>

      The class constructor.

    • Optional additionalData: any

      additional data you want to add to keep trace of a context during serialization in a child class, the goal behind that is to provide a way for serializers extending this one to use custom data across a single object, no matter how deep we are in the object.

    Returns T

    an instance of the class T.

Protected getDeserializePropertyMap

  • getDeserializePropertyMap(obj: any, instance: any): object
  • Returns the fields used to map data properties on result's ones for deserialization.

    Parameters

    • obj: any

      The current object we're deserializing.

    • instance: any

      An instance of the class we're using.

    Returns object

    A custom array containing obj's field as index and corresponding result's field as value.

    • [index: string]: string

Protected prepareSerialize

  • prepareSerialize(obj: any, instance: any): any
  • prepareSerialize(obj: any, instance: any, additionalData: any): any
  • prepareSerialize(obj: any, instance: any, additionalData: any, propertyKey: string): any
  • Prepares an object for serialization, recursively.

    Parameters

    • obj: any

      The object to prepare

    • instance: any

      An instance of the object to prepare for serialization.

    Returns any

  • Prepares an object for serialization, recursively.

    Parameters

    • obj: any

      The object to prepare

    • instance: any

      An instance of the object to prepare for serialization.

    • additionalData: any

      Optional, Additional data you want to pass through recursion.

    Returns any

  • Prepares an object for serialization, recursively.

    Parameters

    • obj: any

      The object to prepare

    • instance: any

      An instance of the object to prepare for serialization.

    • additionalData: any

      Optional, Additional data you want to pass through recursion.

    • propertyKey: string

      Optional, the property key to check, used in recursion.

    Returns any

serialize

  • serialize(data: any, additionalData?: any): string
  • Serialize an object into JSON string, taking Decorators in count for it.

    @FieldName, @SerializeFieldName and @Transient can affect this method.

    Example:

    export class Example{
         @SerializeFieldName('bar')
         foo: string;
    
         @Transient()
         password: string;
    }
    
    const obj = new Example();
    obj.foo = 'baz'
    const result = serializer.serialize(obj);
    console.log(obj);
    

    This will print a JSON string for obj, without password property in it, and foo property renamed to bar.

    Parameters

    • data: any
    • Optional additionalData: any

      additional data you want to add to keep trace of a context during serialization in a child class, the goal behind that is to provide a way for serializers extending this one to use custom data across a single object, no matter how deep we are in the object.

    Returns string

Generated using TypeDoc