EJSONSerializerAdapter
Hierarchy
- EJSONSerializerAdapter
Implements
- Serializer
- Serializer
Index
Constructors
Methods
- clone
- equals
- fromData
- fromJSONValue
- getFactory
- getType
- getTypeKey
- getTypeOrThrow
- getTypes
- getTypesNames
- hasType
- isTypeInstance
- overrideType
- parse
- registerType
- removeType
- removeTypes
- stringify
- toData
- toJSONValue
Constructors
constructor
+ new EJSONSerializerAdapter(typeKey?
: string): EJSONSerializerAdapter
Creates an instance of EJSONSerializerAdapter. Creates an instance of EJSONSerializerAdapter.
Parameters:
Name | Type | Description |
---|---|---|
typeKey? | string | Identifier that will be use to identify custom types on fromData and toData serialization methods. |
Returns: EJSONSerializerAdapter
Methods
clone
▸ clone‹T›(value
: T): T
Implementation of Serializer
Return a deep copy of value.
example
Type parameters:
▪ T
Parameters:
Name | Type | Description |
---|---|---|
value | T | A 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
example
Parameters:
▪ a: any
Base value.
▪ b: any
Other value.
▪Optional
options: object
Additional compering options.
Name | Type |
---|---|
keyOrderSensitive | boolean |
Returns: boolean
fromData
▸ fromData‹T›(data
: Record‹string, any›): T
Implementation of Serializer
Converts record-compatible argument to plain-object data.
example
Type parameters:
▪ T: Serializable
Parameters:
Name | Type | Description |
---|---|---|
data | Record‹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
example
Parameters:
Name | Type | Description |
---|---|---|
value | Record‹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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type 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 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type 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:
Name | Type | Description |
---|---|---|
typeInstance | Serializable | Instance 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type's name for which type will be registered. |
type | Type | Type 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
Parameters:
Name | Type | Description |
---|---|---|
str | string | A 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type's name for which mapping will be created. |
type | Type | Type 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? | boolean | Flag 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:
Name | Type | Description |
---|---|---|
typeName | TypeName | Type 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
Parameters:
▪ value: any
A value or Serializable
instance to stringify.
▪Optional
options: object
Optional serialization options.
Name | Type |
---|---|
canonical | boolean |
indent | boolean | 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
Parameters:
Name | Type | Description |
---|---|---|
serializable | Serializable | Serializable 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
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
example
Parameters:
Name | Type | Description |
---|---|---|
value | any | JSON-compatible value like object or Serializable instance. |
Returns: any
Serialized value as JSON-compatible object without type name($type) identifers preserved.