JsonMapperCompiler
Usage
import { JsonMapperCompiler } from "@tsed/json-mapper";See /packages/specs/json-mapper/src/domain/JsonMapperCompiler.ts.
Overview
abstract class JsonMapperCompiler<Options extends Record<string, any> = any> {
protected cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;
protected types: Map<Type<any>, Type<any>>;
protected typeMappers: Map<any, TypeMapperEntry>;
protected mappers: Record<string, JsonMapperCallback<Options>>;
protected schemes: Record<string, any>;
protected constructors: Record<string, Type<any>>;
protected globals: Record<string, any>;
constructor();
addTypeMapper(model: Type<any> | string, fn: any): this;
removeTypeMapper(model: Type<any> | string): void;
addGlobal(key: string, value: any): this;
compileMapper(mapper: string, { id, groupsId, storeGroups }: {
id: string;
groupsId: string;
model: Type<any> | string;
storeGroups: CachedGroupsJsonMapper<Options>;
}): CachedJsonMapper<Options>;
createContext(options: Options): Options & {
cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;
};
compile(model: Type<any> | string, groups: false | string[], opts?: {
mapper?: any;
}): CachedJsonMapper<Options>;
protected syncTypeMappers<Entry extends TypeMapperEntry>(types: Map<Type | Symbol | string, JsonMapperMethods>, create: (mapper: JsonMapperMethods, model: any, name: string) => Entry, hasChanged: (entry: Entry, mapper: JsonMapperMethods) => boolean): void;
protected execMapper(id: string, value: any, options: Options): any;
protected abstract map(input: any, options: Options): any;
protected abstract alterValue(schemaId: string, value: any, options: Options): any;
protected abstract createMapper(model: Type<any>, id: string, groups: false | string[]): string;
protected resolveType(model: Type<any>): Type<any>;
protected getType(model: Type<any>): Type<any> | MapConstructor | ObjectConstructor | ArrayConstructor | SetConstructor;
protected alterIgnore(id: string, options: Options): any;
protected alterGroups(schema: JsonSchema, groups: false | string[]): boolean;
protected getGroupsId(groups: false | string[]): string;
protected getId(model: Type<any> | string, groupsId: string): string;
protected getSchemaId(id: string, propertyKey: string): string;
}Description
Base compiler that turns schema metadata into executable (de)serialization functions. Subclasses supply map, alterValue, and createMapper implementations to specialize the pipeline for serialization or deserialization.
Constructor
protected constructors: Record<string, Type<any>>;Cached classes by his id
protected cache
protected cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;Cached mappers metadata
protected types
protected types: Map<Type<any>, Type<any>>;Cached result of getType() by model. getType() walks the class ancestors and runs for every value serialized without a schema, so it is resolved once per model.
protected typeMappers
protected typeMappers: Map<any, TypeMapperEntry>;The type mappers registered from the types container, with the function each one was registered with
protected mappers
protected mappers: Record<string, JsonMapperCallback<Options>>;Cached executable mappers by his id
protected schemes
protected schemes: Record<string, any>;Cached schemas
protected globals
protected globals: Record<string, any>;Global variables available in the mapper
addTypeMapper
addTypeMapper(model: Type<any> | string, fn: any): this;removeTypeMapper
removeTypeMapper(model: Type<any> | string): void;addGlobal
addGlobal(key: string, value: any): this;compileMapper
compileMapper(mapper: string, { id, groupsId, storeGroups }: {
id: string;
groupsId: string;
model: Type<any> | string;
storeGroups: CachedGroupsJsonMapper<Options>;
}): CachedJsonMapper<Options>;createContext
createContext(options: Options): Options & {
cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;
};compile
compile(model: Type<any> | string, groups: false | string[], opts?: {
mapper?: any;
}): CachedJsonMapper<Options>;protected syncTypeMappers
protected syncTypeMappers<Entry extends TypeMapperEntry>(types: Map<Type | Symbol | string, JsonMapperMethods>, create: (mapper: JsonMapperMethods, model: any, name: string) => Entry, hasChanged: (entry: Entry, mapper: JsonMapperMethods) => boolean): void;types (
Map<Type|Symbol|string): The type mappers by model.create (
(mapper: JsonMapperMethods): Build the entry, and the function, a type mapper is registered with.hasChanged (
(entry: Entry): Tell whether the mapper changed since its entry was built, beyond being replaced by another mapper.
Register the type mappers of the types container (Array, Set and Map excepted: the compiler maps them itself). A type mapper is registered again only when it is outdated, so map() does not rebuild them on every call.
protected execMapper
protected execMapper(id: string, value: any, options: Options): any;A type mapper must be registered again when it was never registered, when the mapper was replaced or changed, or when its entry was replaced or removed from the cache.
protected abstract map
protected abstract map(input: any, options: Options): any;protected abstract alterValue
protected abstract alterValue(schemaId: string, value: any, options: Options): any;protected abstract createMapper
protected abstract createMapper(model: Type<any>, id: string, groups: false | string[]): string;protected resolveType
protected resolveType(model: Type<any>): Type<any>;protected getType
protected getType(model: Type<any>): Type<any> | MapConstructor | ObjectConstructor | ArrayConstructor | SetConstructor;protected alterIgnore
protected alterIgnore(id: string, options: Options): any;protected alterGroups
protected alterGroups(schema: JsonSchema, groups: false | string[]): boolean;protected getGroupsId
protected getGroupsId(groups: false | string[]): string;protected getId
protected getId(model: Type<any> | string, groupsId: string): string;protected getSchemaId
protected getSchemaId(id: string, propertyKey: string): string;