UNPKG

@mdcc/at-json

Version:

A declarative mapper to and from JSON.

46 lines (45 loc) 1.97 kB
import * as I from './interfaces'; /** * Class for JSON Mapping. * * @export * @class JsonMapper */ export declare class JsonMapper { /** * Serialization method. * Transforms `source` into a new JSON value by applying the "serialization" step for each property decorator. * * Annotated properties are serialized into a property using the `name` value as the destination name (defaults to the property name). * * @param {*} source the value to be serialized. * @returns {string} the transformed JSON value. * @throws An error if a class encountered while serializing has no {@link JsonClass} decorator. * @memberof JsonMapper */ serialize(source: any): any; /** * Deserializes an array by applying {@link deserialize} to each element. * @template T the type of output object * @param {I.Constructable<T>} ctor the destination constructor * @param jsonArray the array to be deserialized * @returns {T} the deserialized object * @memberof JsonMapper */ deserializeArray<T>(ctor: I.Constructable<T>, jsonArray: any[]): T[]; /** * Deserialization method. * Deserializes `source` into an object built using `ctor`. * * Annotated properties are deserialized into a property using the `name` value as the source name (defaults to the property name). * * @template T the type of output object * @param {I.Constructable<T>} ctor the destination constructor * @param {*} source the value to be deserialized * @param {(s: string) => any} stringParser the string parser to deserialize strings into JSON objects. Defaults to `JSON.parse`. * @returns {T} the deserialized object * @throws An error if a class encountered while deserializing has no {@link JsonClass} decorator. * @memberof JsonMapper */ deserialize<T>(ctor: I.Constructable<T>, source: string | object, stringParser?: (s: string) => any): T; }