@venly/venly-core-sdk
Version:
Javascrip/Typescript SDK for Venly's Web3 Services
265 lines • 11.9 kB
TypeScript
import { OperationMode, PropertyConvertingMode, PropertyMatchingRule, ValueCheckingMode } from "./json-convert-enums";
export declare class JsonConvertSettings {
/**
* Determines how the JsonConvert class instance should operate.
*
* You may assign three different values:
* - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
* - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
*/
operationMode: OperationMode;
/**
* Determines which types are allowed to be null.
* This setting may be overridden by property settings (see PropertyConvertingMode).
*
* You may assign three different values:
* - ValueCheckingMode.ALLOW_NULL: all given values are allowed to be null
* - ValueCheckingMode.ALLOW_OBJECT_NULL: objects are allowed to be null, primitive types are not allowed to be null
* - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated
*/
valueCheckingMode: ValueCheckingMode;
deserialize_skipNullValues: boolean;
serialize_skipNullValues: boolean;
/**
* Determines whether primitive types should be checked.
* If true, it will be allowed to assign primitive to other primitive types.
*/
ignorePrimitiveChecks: boolean;
warnIfKeyNotFound: boolean;
/**
* Determines the rule of how JSON properties shall be matched with class properties during deserialization.
*
* You may assign the following values:
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they
* are not case sensitive
*/
propertyMatchingRule: PropertyMatchingRule;
/**
* Determines how nullable property types should be serialized and deserialized.
* Nullable types are either missing (in JSON), undefined (in TypeScript) or null (both).
*
* If the propertyConvertingMode has a non-undefined value, it overrides the individual settings of every property.
*
* The values should be used as follows:
* Determines how nullable property types should be serialized and deserialized.
* Nullable types are either missing (in JSON), undefined (in TypeScript) or null (both).
*
* If the propertyConvertingMode has a non-undefined value, it overrides the individual settings of every property.
*
* The values should be used as follows:
* - MAP_NULLABLE: the mapper is applied, type is checked
* - IGNORE_NULLABLE: the mapper is not applied if the property is missing, undefined or null; the property is
* not added to the result
* - PASS_NULLABLE: the mapper is not applied if the property is missing, undefined or null; the property is
* added with its value to the result
*/
propertyConvertingMode: PropertyConvertingMode | undefined;
}
/**
* Offers a simple API for mapping JSON objects to TypeScript/JavaScript classes and vice versa.
*
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM
*/
export declare class JsonConvert {
private static _converterCache;
private static _defaultSettings;
private static _globalConverters;
static addGlobalConverter(targetType: any, converterType: any): void;
static removeGlobalConverter(targetType: any): boolean;
private static _getConverter;
private static _getObjectProps;
private static _getPropertyMap;
/**
* Tries to serialize a TypeScript object or array of objects to JSON using the mappings defined on
* the specified class reference. Note that if a class reference is provided, it will be used as
* the source of property mapping for serialization, even if the object or one of its elements is
* an instance of a different class with its own mappings. Also, ONLY the properties from the
* class reference will be serialized - any additional properties on the object(s) will be silently
* ignored.
*
* @param data object or array of objects
* @param classReference the class reference which provides the property mappings to use
*
* @returns the JSON object
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static serialize<T extends object, U extends object = {}>(data: T | T[], classReference?: {
new (): U;
}, settings?: JsonConvertSettings | null): string | null;
/**
* Tries to serialize a TypeScript object to a JSON object using either the mappings on the
* provided class reference, if present, or on the provided object. Note that if a class
* reference is provided, it will be used as the source of property mapping for serialization,
* even if the object is itself an instance of a different class with its own mappings.
* Also, ONLY the properties from the class reference will be serialized - any additional
* properties on the object will be silently ignored.
*
* @param data object containing the values to be mapped to a JSON object, must be an
* instance of a class with JSON mappings if no class reference is provided
* @param classReference optional class reference which provides the property mappings to use
*
* @returns the JSON object
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static serializeObject<T extends object, U extends object = {}>(data: T, classReference?: {
new (): U;
}, settings?: JsonConvertSettings | null, ignoreConverter?: boolean): any;
/**
* Tries to serialize a TypeScript array to a JSON array using either the mappings on the
* provided class reference, if present, or on the provided object. Note that if a class
* reference is provided, ALL objects in the array will be serialized using the mappings
* from that class reference, even if they're actually instances of a different class.
* Also, ONLY the properties from the class reference will be serialized - any additional
* properties on the objects will be silently ignored.
*
* @param dataArray array of objects containing the values to be mapped to a JSON object, which
* must be instances of classes with JSON mappings if no class reference is provided
* @param classReference optional class reference which provides the property mappings to use
*
* @returns the JSON array
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static serializeArray<T extends object, U extends object = {}>(dataArray: T[], classReference?: {
new (): U;
}, settings?: JsonConvertSettings | null): any[];
/**
* Tries to deserialize given JSON to a TypeScript object or array of objects.
*
* @param json the JSON as object or array
* @param classReference the class reference
*
* @returns the deserialized data (TypeScript instance or array of TypeScript instances)
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static deserialize<T extends object>(json: object | object[], classReference?: {
new (): T;
} | null, settings?: JsonConvertSettings | null): T | T[];
/**
* Tries to deserialize a JSON object to a TypeScript object.
*
* @param jsonObject the JSON object
* @param classReference the class reference
*
* @returns the deserialized TypeScript instance
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static deserializeObject<T extends object>(jsonObject: any, classReference?: {
new (): T;
} | null, settings?: JsonConvertSettings | null, ignoreConverter?: boolean): T;
/**
* Tries to deserialize a JSON array to a TypeScript array.
*
* @param jsonArray the JSON array
* @param classReference the object class
*
* @returns the deserialized array of TypeScript instances
*
* @throws an Error in case of failure
*
* @see https://www.npmjs.com/package/json2typescript full documentation
*/
static deserializeArray<T extends object>(jsonArray: any[], classReference?: {
new (): T;
} | null, settings?: JsonConvertSettings | null): T[];
/**
* Returns the correct class reference for the provided JSON object.
* If the provided class reference is null, the class reference is retrieved from the class map using the discriminator property.
*
* @param jsonObject the JSON object
* @param classReference the class reference
* @throws throws an Error in case of failure
*/
private static _getRealClassReference;
/**
* Tries to find the JSON mapping for a given class property from the given instance used for mapping,
* and finally assign the value from the given dataObject
*
* @param dataObject the object containing the value to be assigned
* @param instance the instance of the class used for mapping
* @param classPropertyName the property name
* @param json the JSON object
* @throws throws an Error in case of failure
*/
private static _serializeObject_loopProperty;
/**
* Tries to find the JSON mapping for a given class property and finally assign the value.
*
* @param instance the instance of the class
* @param classPropertyName the property name
* @param json the JSON object
*
* @throws throws an Error in case of failure
*/
private static _deserializeObject_loopProperty;
private static _isAnyType;
/**
* Compares the type of a given value with an internal expected json type.
* Either returns the resulting value or throws an exception.
*
* @param expectedType the expected type for the property
* @param value the property value to verify
* @param convertingMode the converting mode for this property
* @param serialize optional param (default: false), if given, we are in serialization mode
*
* @returns returns the resulted mapped property
*
* @throws an error in case of failure
*/
private static _convertProperty;
private static _isJsonObject;
private static _isJsonConverter;
/**
* Gets the value of an object for a given value.
* If the object does not have the specific key, an Error is thrown.
*
* @param data
* @param key
*
* @returns returns the value
*
* @throws an Error in case of the key was not found in the object
*/
private static _getObjectValue;
/**
* Returns a string representation of the expected json type.
*
* @param expectedJsonType the expected type given from the decorator
*
* @returns {string} the string representation
*/
private static _getExpectedType;
/**
* Returns a string representation of the JSON value type.
*
* @param jsonValue the JSON value
*
* @returns {string} the string representation
*/
private static _getJsonType;
/**
* Returns a string representation of the true TypeScript type.
*
* @param trueValue the true value
*
* @returns {string} the string representation
*/
private static _getTrueType;
}
//# sourceMappingURL=json-convert.d.ts.map