import { EntryType, RetrieveCBAsync, ItemType, ValueType, RetrieveCBSync } from "./value"; import { InternalBaseHologram, Hologram, InternalHologram, hologramCtor } from "./hologram"; /** * Serialized values can be of the following types: * - `undefined` * - `string` * - `boolean` * - `SerializedType[]` * * When they are a string, they are prefixed with a type identifier and a colon, e.g. `S:Hello, World!`. * * These prefixes exist: * - `S:` string * - `I:` integer * - `F:` float * - `B:` bytes * - `ID:` ID * - `H:` hologram * * When they are a list, they are an array with the first element being a type identifier, e.g. `['L', 'S:Hello', 'S:World']`. * * These prefixes exist: * - `L:` list * - `T:` tuple * * When they are a map, they are an array with the first element being the map identifier `M`, e.g. `['M', ['name', 'S:Hello'], ['age', 'I:42']]`. * The following elements are key-value pairs. * * This representation is used to serialize and deserialize values, and to distinguish between types that are not * natively supported by JSON (e.g., `ArrayBuffer`, `Hologram`, integer, float, tuple). */ export type SerializedType = undefined | string | boolean | SerializedType[]; /** * A serializable value type. * * This is a type that can be serialized and deserialized to and from a [serialized value]{@link SerializedType}. */ export interface Serializable { deserializeAsync(value: Serialized, hr?: RetrieveCBAsync): Promise; deserializeSync(value: Serialized, hr?: RetrieveCBSync): void; serialize(): Serialized; } /** * A wrapper class for a serialized value. * * This class provides methods to access the value and its entries, and to convert it to a value. */ export declare class Serialized { private _serialized; constructor(serialized: SerializedType); get serialized(): SerializedType; valueAsync(hr?: RetrieveCBAsync): Promise; valueSync(hr?: RetrieveCBSync): T; getEntry(key: K): Serialized>; getItem(key: number): Serialized>; iterator(): Serialized>[]; get defined(): Serialized | undefined; } /** * Converts a value to a serialized value, using the first serializer that returns a defined value. * @param value value * @param sers serializers to try */ export declare function oneOf(value: any, ...sers: ((value: any) => SerializedType)[]): SerializedType; /** * Converts a string to a serialized string. * @param value string * @returns serialized string */ export declare function str(value: string | undefined): SerializedType; /** * Converts a number to a serialized integer. * @param value integer number * @returns serialized integer */ export declare function int(value: number | undefined): SerializedType; /** * Converts a number to a serialized float. * @param value floating number * @returns serialized float */ export declare function float(value: number | undefined): SerializedType; /** * Converts a boolean to a serialized boolean. * @param value boolean * @returns serialized boolean */ export declare function bool(value: boolean | undefined): SerializedType; /** * Converts an ID to a serialized ID. * @param value ID * @returns serialized ID */ export declare function id(value: string | undefined): SerializedType; /** * Converts an array buffer to serialized bytes. * @param value Array buffer * @returns serialized bytes */ export declare function bytes(value: ArrayBuffer | undefined): SerializedType; /** * Converts a hologram to a serialized hologram. * @param value Hologram (internal or external) * @returns serialized hologram */ export declare function holo(value: InternalBaseHologram | Hologram | undefined): SerializedType; /** * Converts a map of serialized values to a serialized map. * @param value map of serialized values * @returns serialized map */ export declare function map(value: { [_: string]: SerializedType; } | undefined): SerializedType; /** * Converts a list of serialized values to a serialized list. * @param value list of serialized values * @returns serialized list */ export declare function list(value: SerializedType[] | undefined): SerializedType; /** * Converts a list of serialized values to a serialized tuple. * @param value list of serialized values * @returns serialized tuple */ export declare function tuple(value: SerializedType[] | undefined): SerializedType; /** * Converts a serializable value to its serialized value. * @param value serializable value * @returns serialized value */ export declare function ser(value: Serializable | undefined): SerializedType; /** * Converts a list of serializable values to a serialized list, using the given serializer. * @param fun serializer * @returns serialized list */ export declare function listOf(fun: (value: any) => SerializedType): (values: (Serializable | undefined)[] | undefined) => SerializedType; /** * Converts a list of serializable values to a serialized list. * @param values list of serializable values * @returns serialized list */ export declare function serList(values: (Serializable | undefined)[] | undefined): SerializedType; /** * Converts a map of serializable values to a serialized map, using the given serializer. * @param value object whose entries are serializable * @returns serialized map */ export declare function serMap(value: { [_: string]: Serializable | undefined; } | undefined): SerializedType; /** * Takes a values and tries to serialize it. * * If the value is undefined, it returns undefined. * * If the value is a serialized value, it returns it. * * If the value is a string, it checks if it is a serialized value, following the {@link SerializedType}. * If it is, it returns it. If it is not, it returns a serialized string. * * If the value is a number, it returns a serialized integer. * * If the value is a boolean, it returns a serialized boolean. * * If the value is null, it throws an error. * * If the value is an array buffer, it returns serialized bytes. * * If the value is a hologram, it returns a serialized hologram. * * If the value is an array, it checks if it is a serialized value, following the {@link SerializedType}. * If it is, it returns it. If it is not, it returns a serialized list. * * If the value is an object, it returns a serialized map. * * If the value is none of the above, it throws an error. * * @param value value to serialize or a serialized value * @returns serialized value */ export declare function serialize(value: any): SerializedType; /** * Deserializes a serialized value. * @param serialized Serialized value * @param hr Hologram retrieve callback */ export declare function deserializeAsync(serialized: SerializedType, hr?: RetrieveCBAsync): Promise; /** * Deserializes a serialized value synchronously. * @param serialized Serialized value * @param hr Hologram retrieve callback */ export declare function deserializeSync(serialized: SerializedType, hr?: RetrieveCBSync): T; /** * Converts an internal hologram to a hologram of the given type. * @param holo Internal hologram * @param ctor Hologram type */ export declare function toHoloOpt(holo: InternalHologram | undefined, ctor: hologramCtor): T | undefined; /** * Converts an internal hologram to a hologram of the given type. * @param holo Internal hologram * @param ctor Hologram type */ export declare function toHolo(holo: InternalHologram, ctor: hologramCtor): T;