EJSONSerializerAdapter

Hierarchy

  • EJSONSerializerAdapter

Implements

Index

Constructors

Methods

Constructors

constructor

+ new EJSONSerializerAdapter(typeKey?: string): EJSONSerializerAdapter

Creates an instance of EJSONSerializerAdapter. Creates an instance of EJSONSerializerAdapter.

Parameters:

NameTypeDescription
typeKey?stringIdentifier that will be use to identify custom types on fromData and toData serialization methods.

Returns: EJSONSerializerAdapter

Methods

clone

cloneT›(value: T): T

Implementation of Serializer

Return a deep copy of value.

example

@define('Address')
class Address extends Serializable {
city: string;
street: string;
}
@define('Person')
class Person extends Serializable {
firstName: string;
lastName: string;
address: Address;
}
const address = new Address({
city: 'New York',
street: 'Wall Street',
});
const person = new Person({
firstName: 'Jane',
lastName: 'Doe',
address,
});
const clonedPerson = serializer.clone<Person>(person);
expect(clonedPerson).to.be.instanceof(Person);
expect(clonedPerson).to.be.eql(person);
expect(clonedPerson).to.not.be.equal(person);
expect(clonedPerson.address).to.not.be.equal(address);

Type parameters:

T

Parameters:

NameTypeDescription
valueTA value to copy.

Returns: T

Cloned value without any reference


equals

equals(a: any, b: any, options?: object): boolean

Return true if a and b are equal to each other. Return false otherwise. Uses the equals method on a if present, otherwise performs a deep comparison.

example

@define('Car')
class Car extends Serializable {
brand: string;
}
const carA = new Car({
brand: 'Audi',
});
const carB = new Car({
brand: 'Audi',
});
expect(serializer.equals(carA, carB)).to.be.true;

example

@define('Car')
class Car extends Serializable {
brand: string;
}
const carA = new Car({
brand: 'Audi',
});
const carB = new Car({
brand: 'BMW',
});
expect(serializer.equals(carA, carB)).to.be.false;

Parameters:

a: any

Base value.

b: any

Other value.

Optional options: object

Additional compering options.

NameType
keyOrderSensitiveboolean

Returns: boolean


fromData

fromDataT›(data: Record‹string, any›): T

Implementation of Serializer

Converts record-compatible argument to plain-object data.

example

@define('Car')
class Car extends Serializable {
brand: string;
}
const data = {
_type: 'Car',
brand: 'Bentley',
};
const typeInstance = serializer.fromData(data);
expect(typeInstance).to.be.instanceof(Car);
expect(typeInstance).to.be.eql(
new Car({
brand: 'Bentley',
})
);

Type parameters:

T: Serializable

Parameters:

NameTypeDescription
dataRecord‹string, any›Data as an object.

Returns: T

Converted data to Serializable instance.


fromJSONValue

fromJSONValue(value: Record‹string, any›): Record‹string, any› | Serializable

Implementation of Serializer

Deserializes an EJSON value from its plain JSON representation.

throws {TypeNotFoundError} Thrown if the value contains a serialized type that is not supported.

example

@define('Address')
class Address extends Serializable {
city: string;
street: string;
}
@define('Person')
class Person extends Serializable {
firstName: string;
lastName: string;
address: Address;
}
const json = {
$type: 'Person',
$value: {
firstName: 'Jane',
lastName: 'Doe',
address: {
$type: 'Address',
$value: {
city: 'New York',
street: 'Wall Street',
},
},
},
};
const person = serializer.fromJSONValue(json);
expect(person).to.be.instanceof(Person);
expect(person).to.be.eql(
new Person({
firstName: 'Jane',
lastName: 'Doe',
address: new Address({
city: 'New York',
street: 'Wall Street',
}),
})
);

example

const date = new Date('December 17, 1995 03:24:00');
const obj = {
$date: date,
};
const json = serializer.toJSONValue(obj);
expect(serializer.fromJSONValue(json)).to.be.eql(date);

Parameters:

NameTypeDescription
valueRecord‹string, any›A value to deserialize into EJSON.

Returns: Record‹string, any› | Serializable

Deserialized EJSON value.


getFactory

getFactory(typeName: TypeName): Function & object

Returns factory for type name.

Parameters:

NameTypeDescription
typeNameTypeNameType name for type.

Returns: Function & object

Factory function for type.


getType

getType(typeName: TypeName): Type | undefined

Implementation of Serializer

Returns type for type name.

Parameters:

NameTypeDescription
typeNameTypeNameType name for type.

Returns: Type | undefined

Type constructor, else undefined;


getTypeKey

getTypeKey(): string

Returns type key identifier.

Returns: string

Type key identifier as a string.


getTypeOrThrow

getTypeOrThrow(typeName: TypeName): Type

Implementation of Serializer

Returns type for type name.

throws {TypeNotFoundError} Thrown if type for type name can't be found.

Parameters:

NameTypeDescription
typeNameTypeNameType name for type.

Returns: Type

Type constructor, else throws;


getTypes

getTypes(): Map‹TypeName, Type

Implementation of Serializer

Returns all data-types registered on EJSON.

Returns: Map‹TypeName, Type

Returns object with relation typeName:type.


getTypesNames

getTypesNames(): TypeName[]

Implementation of Serializer

Returns all types names.

Returns: TypeName[]

List of type names of all registered types.


hasType

hasType(typeName: TypeName): boolean

Implementation of Serializer

Evaluates if serializer has registered type by type name.

Parameters:

NameTypeDescription
typeNameTypeNameType name for type.

Returns: boolean

Returns true if type is registered, else false.


isTypeInstance

isTypeInstance(typeInstance: Serializable): boolean

Implementation of Serializer

Evaluates if provided instance belongs to one of custom types.

Parameters:

NameTypeDescription
typeInstanceSerializableInstance of a type implementing Serializable interface.

Returns: boolean

Returns true if instance is of registered type, else false.


overrideType

overrideType(typeName: TypeName, type: Type): void

Implementation of Serializer

Override a data-type on serializer.

Parameters:

NameTypeDescription
typeNameTypeNameType's name for which type will be registered.
typeTypeType constructor implementing Serializable interface for registration.

Returns: void


parse

parse(str: string): any

Implementation of Serializer

Parse a string into an EJSON value.

throws {UnparsableValueError} Thrown if the argument is not a valid EJSON.

example

@define('Address')
class Address extends Serializable {
city: string;
street: string;
}
@define('Person')
class Person extends Serializable {
firstName: string;
lastName: string;
address: Address;
}
const string =
'{"$type":"Person","$value":{"firstName":"Jane","lastName":"Doe","address":{"$type":"Address","$value":{"city":"New York","street":"Wall Street"}}}}';
const ejsonValue = serializer.parse(string);
expect(ejsonValue).to.be.instanceof(Person);
expect(ejsonValue).to.be.eql(
new Person({
firstName: 'Jane',
lastName: 'Doe',
address: new Address({
city: 'New York',
street: 'Wall Street',
}),
})
);

Parameters:

NameTypeDescription
strstringA string to parse into an EJSON value.

Returns: any

Parsed value to type or Serializable instance.


registerType

registerType(typeName: TypeName, type: Type, shouldOverride?: boolean): void

Implementation of Serializer

Register a data-type on serializer.

throws {UnregistrableTypeError} Thrown if type does not implement Serializable interface.

throws {TypeExistsError} Thrown if type would overridden on EJSON without explicit call.

Parameters:

NameTypeDescription
typeNameTypeNameType's name for which mapping will be created.
typeTypeType constructor implementing Serializable interface for registration. Must contain typeName - a tag for your custom type that must be unique among other data types defined in your project.
shouldOverride?booleanFlag indicating that type should be overridden if exist.

Returns: void


removeType

removeType(typeName: TypeName): void

Implementation of Serializer

Removes data-type by its type name.

Parameters:

NameTypeDescription
typeNameTypeNameType name for type.

Returns: void


removeTypes

removeTypes(): void

Implementation of Serializer

Allows to remove all data-types from EJSON.

Returns: void


stringify

stringify(value: any, options?: object): string

Serialize a value to a string with value's initial type preserved.

example

@define('Address')
class Address extends Serializable {
city: string;
street: string;
}
@define('Person')
class Person extends Serializable {
firstName: string;
lastName: string;
address: Address;
}
const person = new Person({
firstName: 'Jane',
lastName: 'Doe',
address: new Address({
city: 'New York',
street: 'Wall Street',
}),
});
expect(serializer.stringify(person)).to.be.equal(
'{"$type":"Person","$value":{"firstName":"Jane","lastName":"Doe","address":{"$type":"Address","$value":{"city":"New York","street":"Wall Street"}}}}'
);

Parameters:

value: any

A value or Serializable instance to stringify.

Optional options: object

Optional serialization options.

NameType
canonicalboolean
indentboolean | number

Returns: string

Stringified value.


toData

toData(serializable: Serializable): Record‹string, any›

Implementation of Serializer

Converts Serializable to plain-object data.

throws {UnregistrableTypeError} Thrown if provided argument is not a type implementing Serializable interface.

example

@define('Car')
class Car extends Serializable {
brand: string;
}
const car = new Car({
brand: 'Bentley',
});
expect(serializer.toData(car)).to.be.eql({
_type: 'Car',
brand: 'Bentley',
});

Parameters:

NameTypeDescription
serializableSerializableSerializable instance.

Returns: Record‹string, any›

Converted Serializable to plain-object data.


toJSONValue

toJSONValue(value: any): any

Implementation of Serializer

Serializes value into a JSON-compatible value. It preserves all custom fields, however the initial value type is not saved.

remarks Method toJSONValue is not returning type serialized in object structure like

{$type: "MyType", $value: {key: "my-string"}}

Since that would impact stringify method that under the hood uses toJSONValue. Method stringify will produce string that has exact structure like presented above.

example

@define('Address')
class Address extends Serializable {
city: string;
street: string;
}
@define('Person')
class Person extends Serializable {
firstName: string;
lastName: string;
address: Address;
}
const person = new Person({
firstName: 'Jane',
lastName: 'Doe',
address: new Address({
city: 'New York',
street: 'Wall Street',
}),
});
expect(person.toJSONValue()).to.be.eql({
firstName: 'Jane',
lastName: 'Doe',
address: {
city: 'New York',
street: 'Wall Street',
},
});

example

const date = new Date();
const obj = {
$date: date,
};
expect(
serializer.toJSONValue(obj)
).to.be.eql({ $date: date.toJSON() });

Parameters:

NameTypeDescription
valueanyJSON-compatible value like object or Serializable instance.

Returns: any

Serialized value as JSON-compatible object without type name($type) identifers preserved.