UNPKG

@mdcc/at-json

Version:

A declarative mapper to and from JSON.

109 lines (108 loc) 3.27 kB
import { JsonMapper } from './mapper'; export declare const Symbols: { mappingMetadata: symbol; mappingOptions: symbol; fieldsMetadata: symbol; metadataRoot: symbol; }; /** * Type alias for a mapping function. */ export type Mapping<T = any, R = any> = (mapper: JsonMapper, val: T) => R; /** * Interface for constructor class. * * @export * @interface Constructable * @template T the constructed type */ export type Constructable<T, Args extends any[] = any[]> = new (...args: Args) => T; /** * Interface for classes that want to apply a custom serialization logic. * If a class implements this interface, its {@link customSerialize} * method will be called instead of the default serialization logic. * It is the responsibility of the implementation to recursively serialize * nested objects. */ export interface CustomSerialize { /** * Custom serialization logic. */ customSerialize(mapper: JsonMapper): any; } /** * Interface for classes that want to apply an additional deserialization logic * after default deserialization. * If a class implements this interface, its {@link afterDeserialize} * method will be called after the deserialization. */ export interface AfterDeserialize { /** * Additional deserialization logic. */ afterDeserialize(): void; } /** * Type guard for {@link CustomSerialize} interface. * * @param mapValue value to check * @returns if the parameter is a CustomSerialize interface */ export declare function hasCustomSerializeExport(mapValue: any): mapValue is CustomSerialize; /** * Type guard for {@link AfterDeserialize} interface. * * @param mapValue value to check * @returns if the parameter is a AfterDeserialize interface */ export declare function hasAfterDeserialize(mapValue: any): mapValue is AfterDeserialize; /** * Mapping functions. * * @export * @interface IMappingFunctions * @template S the type of the deserialized property * @template D the type of the serialized property */ export interface IMappingFunctions<S = any, D = any> { /** * Custom deserialization function. * * @type {Mapping} * @memberof IMappingOptions */ deserialize?: Mapping<S, D>; /** * Custom serialization function. * * @type {Mapping} * @memberof IMappingOptions */ serialize?: Mapping<D, S>; } /** * Mapping extra options. * * @export * @interface IMappingOptionsExtra */ export interface IMappingOptionsExtra { /** * Property name. * If specified, the serialize process will convert the class property name to this value, and * the deserialize process will convert the other way. * * @type {string} * @memberof IMappingOptions */ name?: string; } export type IMappingOptions<S = any, D = any> = IMappingFunctions<S, D> & IMappingOptionsExtra; /** * Decorator input for decorators that support custom serialize/deserialize functions. */ export type DecoratorInputWithCustomFunctions<S = any, D = any> = string | IMappingOptions<S, D> | undefined; /** * Decorator input for decorators that don't support custom serialize/deserialize functions. */ export type DecoratorInputWithoutCustomFunctions = string | IMappingOptionsExtra | undefined;