Skip to content

JsonMapperCompiler

@tsed/json-mapper

Usage ​

typescript
import { JsonMapperCompiler } from "@tsed/json-mapper";

See /packages/specs/json-mapper/src/domain/JsonMapperCompiler.ts.

Overview ​

ts
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 ​

ts
protected constructors: Record<string, Type<any>>;

Cached classes by his id

protected cache ​

ts
protected cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;

Cached mappers metadata

protected types ​

ts
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 ​

ts
protected typeMappers: Map<any, TypeMapperEntry>;

The type mappers registered from the types container, with the function each one was registered with

protected mappers ​

ts
protected mappers: Record<string, JsonMapperCallback<Options>>;

Cached executable mappers by his id

protected schemes ​

ts
protected schemes: Record<string, any>;

Cached schemas

protected globals ​

ts
protected globals: Record<string, any>;

Global variables available in the mapper

addTypeMapper ​

ts
addTypeMapper(model: Type<any> | string, fn: any): this;

removeTypeMapper ​

ts
removeTypeMapper(model: Type<any> | string): void;

addGlobal ​

ts
addGlobal(key: string, value: any): this;

compileMapper ​

ts
compileMapper(mapper: string, { id, groupsId, storeGroups }: {
     id: string;
     groupsId: string;
     model: Type<any> | string;
     storeGroups: CachedGroupsJsonMapper<Options>;
 }): CachedJsonMapper<Options>;

createContext ​

ts
createContext(options: Options): Options & {
     cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>;
 };

compile ​

ts
compile(model: Type<any> | string, groups: false | string[], opts?: {
     mapper?: any;
 }): CachedJsonMapper<Options>;

protected syncTypeMappers ​

ts
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 ​

ts
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 ​

ts
protected abstract map(input: any, options: Options): any;

protected abstract alterValue ​

ts
protected abstract alterValue(schemaId: string, value: any, options: Options): any;

protected abstract createMapper ​

ts
protected abstract createMapper(model: Type<any>, id: string, groups: false | string[]): string;

protected resolveType ​

ts
protected resolveType(model: Type<any>): Type<any>;

protected getType ​

ts
protected getType(model: Type<any>): Type<any> | MapConstructor | ObjectConstructor | ArrayConstructor | SetConstructor;

protected alterIgnore ​

ts
protected alterIgnore(id: string, options: Options): any;

protected alterGroups ​

ts
protected alterGroups(schema: JsonSchema, groups: false | string[]): boolean;

protected getGroupsId ​

ts
protected getGroupsId(groups: false | string[]): string;

protected getId ​

ts
protected getId(model: Type<any> | string, groupsId: string): string;

protected getSchemaId ​

ts
protected getSchemaId(id: string, propertyKey: string): string;

Released under the MIT License.