import { InjectionToken } from '@angular/core'; import { Schema } from '@lightweightform/storage'; import { LfSerializer } from '../abstract/value-serializer'; import * as i0 from "@angular/core"; /** * Function used to (de)serialise a value. * @param value Value to (de)serialise. * @param schema Schema of the value being (de)serialised. * @param path Path of the value being (de)serialised. * @returns (De)serialised value (not necessarily the last form since the value * will be passed to the next (de)serialiser). */ export declare type LfJsonTransformationFn = (value: any, schema: Schema, path: string) => any; /** * Type of object used to apply transformations during a (de)serialisation. */ export interface LfJsonTransformer { /** * Serialisation function. */ serialize?: LfJsonTransformationFn; /** * Deserialisation function. */ deserialize?: LfJsonTransformationFn; } /** * Options used to configure the JSON serialiser. */ export interface LfJsonSerializerOptions { /** * Character encoding to set as metadata when saving the file and used to * decode files. Defaults to `utf-8`. */ charset?: string; /** * Whether to automatically add an UTF BOM (not applied to IE9-). Defaults to * `true`. */ autoBOM?: boolean; /** * A function used to encode the content according to the intended charset. * This is something that JavaScript cannot natively do (if not provided, the * provided charset will be set as file metadata but the content will be * encoded as `utf-8`). */ encode?: (content: string) => BlobPart[]; /** * Space used when printing the value as a JSON string. Defaults to `0` (no * spaces) or `'\t'` (use tabs as spaces) depending on whether the app is * running in production mode. */ space?: number | string; /** * Simple transformer or list of simple transformers: a simple transformer is * an object which provides functions to (de)serialise non-`null` values of * simple schemas (boolean, date, number, and string schemas). * * When a list of simple transformers is provided, they run sequentially (in * reverse order when deserialising). These \[serialisers|deserialisers] will * run \[before|after] the \[serialiser|deserialiser] defined in a schema's * `jsonSimpleTransform` respectively. * * Return `undefined` or the provided value to perform no transformation. The * JSON serialiser service will perform its standard transformation when the * value remains the same after all simple transformations run. * * The following example serialises booleans as "yes/no": * ```typescript * { * simpleTransform: { * serialize: (val, schema, path) => { * if (isBooleanSchema(schema)) { * return val ? 'yes' : 'no'; * } * }, * deserialize: (val, schema, path) => { * if (isBooleanSchema(schema)) { * return val === 'yes'; * } * }, * } * } * ``` */ simpleTransform?: LfJsonTransformer | LfJsonTransformer[]; /** * Transformer or list of transformers: a transformer is an object providing * functions to (de)serialise values. Serialisers transform values after they * have been serialised by the JSON serialiser service but before they are * `JSON.stringify`ed. Deserialisers transform values after they have been * `JSON.parse`d but before they are deserialised by the JSON serialiser * service. * * When a list of transformers are provided, they will run sequentially (in * reverse order when deserialising). These serialisers/deserialisers will run * before/after the serialiser/deserialiser defined in a schema's * `jsonTransform` respectively. To perform no transformation, functions * should return the provided value untouched. */ transform?: LfJsonTransformer | LfJsonTransformer[]; } /** * Extra properties that can be added to any schema to configure how its values * should be serialised by the JSON serialiser. */ export interface LfJsonSerializerSchemaProperties { /** * Simple transformer: an object which provides functions to (de)serialise * non-`null` values of simple schemas (boolean, date, number, and string * schemas). * * This \[serialiser|deserialiser] will run \[after|before] the * \[serialisers|deserialisers] defined in the JSON serialiser service * `simpleTransform` option respectively. */ jsonSimpleTransform?: LfJsonTransformer; /** * Transformer: an object providing functions to (de)serialise values of this * schema. The serialiser transforms values after they have been serialised by * the JSON serialiser service but before they are `JSON.stringify`ed. The * deserialiser transforms values after they have been `JSON.parse`d but * before they are deserialised by the JSON serialiser service. * * This \[serialiser|deserialiser] will run \[after|before] the * \[serialisers|deserialisers] defined in the JSON serialiser service * `transform` option respectively. To perform no transformation, functions * should return the provided value untouched. */ jsonTransform?: LfJsonTransformer; } /** * Injector used to set the JSON serialiser options. */ export declare const LF_JSON_SERIALIZER_OPTIONS: InjectionToken; /** * Transformer used to save/load application values as JSON. */ export declare class LfJsonSerializer extends LfSerializer { charset: string; mimeType: string; accept: string; extension: string; autoBOM: boolean; encode?: (content: string) => BlobPart[]; /** * Space used when printing the value as a JSON string. */ space: number | string; /** * Simple transform(s). */ simpleTransform?: LfJsonTransformer | LfJsonTransformer[]; /** * Transform(s). */ transform?: LfJsonTransformer | LfJsonTransformer[]; constructor(options: LfJsonSerializerOptions | null); /** * Sets the serialiser's options. * @param options Options to set. */ setOptions(options: LfJsonSerializerOptions): void; serialize(js: any, schema: Schema & LfJsonSerializerSchemaProperties, path: string): string; deserialize(text: string, schema: Schema & LfJsonSerializerSchemaProperties, path: string): any; /** * Convert a storage-compatible JS value into a JSON-compatible value (handle * dates and maps, especially). * @param js Storage-compatible JS value to convert into a JSON-compatible * value. * @param schema Schema of the value to convert. * @param path Path of the value to convert. * @returns JSON-compatible version of provided value. */ jsToJson(js: any, schema: Schema & LfJsonSerializerSchemaProperties, path: string): any; /** * Convert a JSON-compatible value into a storage-compatible JS value (handle * dates and maps, especially). * @param json JSON-compatible value to convert into a storage-compatible JS * value. * @param schema Schema of the value to convert. * @param path Path of the value to convert. * @returns Storage-compatible JS value. */ jsonToJs(json: any, schema: Schema & LfJsonSerializerSchemaProperties, path: string): any; /** * Runs all simple serialisers for a value of a simple schema. * @param value Value on which to run simple serialisers. * @param schema Schema of value on which to run serialisers. * @param path Path of value on which to run serialisers. * @returns Serialised value. */ private runSimpleSerializers; /** * Runs all simple deserialisers for a value of a simple schema. * @param value Value on which to run simple deserialisers. * @param schema Schema of value on which to run deserialisers. * @param path Path of value on which to run deserialisers. * @returns Deserialised value. */ private runSimpleDeserializers; /** * Runs all serialisers for a JSON value of a given schema. * @param value JSON value on which to run serialisers. * @param schema Schema of value on which to run serialisers. * @param path Path of value on which to run serialisers. * @returns Serialised JSON value. */ private runSerializers; /** * Runs all deserialisers for a JSON value of a given schema. * @param value JSON value on which to run deserialisers. * @param schema Schema of value on which to run deserialisers. * @param path Path of value on which to run deserialisers. * @returns Deserialised JSON value. */ private runDeserializers; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; }