Options
All
  • Public
  • Public/Protected
  • All
Menu

@kaiu/serializer

Index

Functions

DeserializeAs

  • DeserializeAs(clazz: Class | [Class]): function
  • Tags a property to be deserialized as a given class.

    Example:

    export class Foo{
         @DeserializeAs(Bar)
         bar:Bar;
    }
    
    decorator

    Property

    Parameters

    • clazz: Class | [Class]

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

DeserializeFieldName

  • DeserializeFieldName(fieldName: string): function
  • Tags a property to map from a different field name upon deserialization.

    Example:

    export class Example{
         @DeserializeFieldName('bar')
         foo: string;
    }
    
    const result = serializer.deserialize<Example>({ bar: 'hey' }, Example);
    console.log(result.foo);
    

    This will print hey because the attribute with name bar has been mapped into the foo property.

    decorator

    Property

    Parameters

    • fieldName: string

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

FieldName

  • FieldName(fieldName: string): function
  • Tags a property to map from a different field name.

    Example:

    export class Example{
         @FieldName('bar')
         foo: string;
    }
    
    const result = serializer.deserialize<Example>({ bar: 'hey' }, Example);
    console.log(result.foo);
    

    This will print hey because the attribute with name bar has been mapped into the foo property.

    decorator

    Property

    Parameters

    • fieldName: string

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

Parent

  • Flags the given property as the discriminator.

    Example:

    @Parent({
         discriminatorField: 'type',
         allowSelf: true // This one is optional.
    })
    export class Vehicle{
         type: string;
         color: Color;
    }
    

    When the serializer will deserialize using this class, he will use Vehicle.type value to find the right child.

    You can se the allowSelf flag to true if you want to get an instance of this class if no subclass is found with the current discriminator value instead of an error.

    decorator

    Class

    Parameters

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

SerializeFieldName

  • SerializeFieldName(fieldName: string): function
  • Tags a property to map from a different field name upon serialization.

    Example:

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

    This will print the object as if foo property was named bar.

    decorator

    Property

    Parameters

    • fieldName: string

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

Transient

  • Transient(): function
  • Tags a property as transient, meaning that it won't be used in serialization.

    Example:

    export class Example {
         foo: string;
    
         @Transient()
         password: string;
    }
    
    const example = new Example();
    example.foo = 'bar';
    example.password = 'Super secret';
    console.log(serializer.serialize(example));
    

    This will print a JSON string of example object, without password property in it.

    decorator

    Property

    Returns function

      • (...args: any[]): void
      • Parameters

        • Rest ...args: any[]

        Returns void

Generated using TypeDoc