Serialise

source

Functions 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

staticany(value: JsonValue): JsonValuesource

The identity function.

staticarray<T>(elementSerialiser: SerialiseFn): SerialiseFnsource

Returns a serialiser that serialises an array, applying the given elementSerialiser to each elementSerialiser` to each element.

staticdate(value: PlainDate): stringsource

Serialises a Temporal.PlainDate to a string in YYYY-MM-DD format.

staticenum<Domain extends EnumDomain>(domain: Domain): SerialiseFnsource

Returns a serialiser that serialises a value of the given enumeration domain into a string.

staticmap<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.

staticnullable<T>(serialiseFn: SerialiseFn): SerialiseFnsource

Wraps the given serialisation function so that it accepts null and undefined, returning null for either.

staticoneOf<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, or null) 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.

staticset<T>(elementSerialiser: SerialiseFn): SerialiseFnsource

Returns a serialiser that serialises a Set into an array, applying the given elementSerialiser to each element of the set.

statictimestamp(value: Instant): stringsource

Serialises a Temporal.Instant into a string in ISO8601 timestamp format with UTC timezone and microsecond precision.

statictoArrayUsingQueryMetadata<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.

statictoObjectUsingMetadata<T>(metadata: ReflectionMetadata): SerialiseFnsource

Returns a serialiser that serialises an object using the given reflection metadata.

statictuple<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.

staticunwrappedTuple<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.

staticyearMonth(value: PlainYearMonth): stringsource

Serialises a Temporal.PlainYearMonth to a string in YYYY-MM format.