Serialise
sourceFunctions for serialising in-memory values of SDL-supported types into their corresponding wire-encodable form.
Each serialisation function accepts a value of a specific type and returns a value that is safe to serialise to JSON.
Serialisers may not throw or reject values.
For more information on SDL datatypes and their serialisation, refer to: https://gitlab.cartax.io/platform/sdl/-/blob/master/docs/datatypes.md
Constructors
new Serialise(): Serialise
Methods
Static methods
any(value: JsonValue): JsonValuesource
The identity function.
array<T>(elementSerialiser: SerialiseFn): SerialiseFnsource
Returns a serialiser that serialises an array, applying the given elementSerialiser to each elementSerialiser` to each element.
date(value: PlainDate): stringsource
Serialises a Temporal.PlainDate to a string in YYYY-MM-DD format.
enum<Domain extends EnumDomain>(domain: Domain): SerialiseFnsource
Returns a serialiser that serialises a value of the given enumeration domain into a string.
map<K, V>(serialiseKey: Function, serialiseValue: Function): SerialiseFnsource
Returns a serialiser that serialises a Map into an object by applying the given key and value serialiser functions.
nullable<T>(serialiseFn: SerialiseFn): SerialiseFnsource
Wraps the given serialisation function so that it accepts null and undefined, returning null for either.
oneOf<T extends TypeMap>(serialiserMap: SerialiserMap<T>): SerialiseFnsource
Returns a serialiser that serialises a sum type by using the given tag mapping. The serialised form is one of the following:
Literal: primitive types (
boolean,number,string, ornull) may be directly stored in the field.Tagged: for a struct type, the value is stored as an object with a single entry. The key of the entry is the 'tag' of the type; this is either explicitly defined by the model author, or defaults to the name of the struct type.
Each type may appear at most once in the tag map.
set<T>(elementSerialiser: SerialiseFn): SerialiseFnsource
Returns a serialiser that serialises a Set into an array, applying the given elementSerialiser to each element of the set.
timestamp(value: Instant): stringsource
Serialises a Temporal.Instant into a string in ISO8601 timestamp format with UTC timezone and microsecond precision.
toArrayUsingQueryMetadata<T>(query: QueryReflectionMetadata): SerialiseFnsource
Returns a serialiser that serialises an object to an array of values, using the given reflection metadata. Allows for a subset of fields to be serialised.
toObjectUsingMetadata<T>(metadata: ReflectionMetadata): SerialiseFnsource
Returns a serialiser that serialises an object using the given reflection metadata.
tuple<Ts extends any[]>(serialisers: SerialiserTuple<Ts>): SerialiseFnsource
Returns a serialiser that serialises an Array of a fixed size using a bijective mapping of value to serialiser.
unwrappedTuple<Ts extends any[]>(serialisers: SerialiserTuple<Ts>): SerialiseFnsource
Returns a serialiser that serialises either an Array or a single value. This is the same as tuple, except that a 1-tuple is not enclosed in an array.
yearMonth(value: PlainYearMonth): stringsource
Serialises a Temporal.PlainYearMonth to a string in YYYY-MM format.