Deserialize an object into a specified class.
For an array of objects, the type specified should be an array of the class.
serializer.deserialize({ prop: 'bar' }, Foo); // -> Foo
serializer.deserialize([{ prop: 'bar' }, {prop: 'baz'}], [Foo]); // -> [Foo, Foo]
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.
serializer.deserialize({ prop: 'bar' }, Foo); // -> Foo
serializer.deserialize([{ prop: 'bar' }, {prop: 'baz'}], [Foo]); // -> [Foo, Foo]
an array of instances of the class T
.
Deserialize an array of objects into an array of specified classes.
The array of objects.
The class constructor.
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.
An array of instances of the type T
.
Deserialize an object into a specified class.
The object.
The class constructor.
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.
an instance of the class T
.
Returns the fields used to map data properties on result's ones for deserialization.
The current object we're deserializing.
An instance of the class we're using.
A custom array containing obj's field as index and corresponding result's field as value.
Prepares an object for serialization, recursively.
The object to prepare
An instance of the object to prepare for serialization.
Prepares an object for serialization, recursively.
The object to prepare
An instance of the object to prepare for serialization.
Optional, Additional data you want to pass through recursion.
Prepares an object for serialization, recursively.
The object to prepare
An instance of the object to prepare for serialization.
Optional, Additional data you want to pass through recursion.
Optional, the property key to check, used in recursion.
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
.
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.
Generated using TypeDoc
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.