UNPKG

tsbase

Version:

Base class libraries for TypeScript

122 lines 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonSerializer = void 0; const Strings_1 = require("../../System/Strings"); /** * Keys to check for simple values when data is structured as shown here: * example: field: [ { value: '1' } ] */ const keysToCheck = [ 'value', 'target_id' ]; /** * Deserializes raw json data into an instance of T * **Some conventions are necessary - see wiki for full details** * - No ***required*** constructor params * - Init all property values * - Array<T> / List<T> values must be initialized with a single instance inside */ class JsonSerializer { Deserialize(t, data) { const object = new t(); const classProperties = Object.keys(object); const jsonKeys = Object.keys(data); jsonKeys.forEach(element => { const instanceKey = this.getInstanceKey(classProperties, element); const jsonElement = data[element]; if (instanceKey !== Strings_1.Strings.Empty && jsonElement) { const property = object[instanceKey]; if (this.propertyIsSimple(property)) { this.serializeSimpleField(jsonElement, object, instanceKey); } else if (this.propertyIsArrayOfObjects(property)) { const values = this.getArrayValuesFromSerializer(property[0], jsonElement); object[instanceKey] = values; } else { const json = Array.isArray(jsonElement) ? jsonElement[0] : jsonElement; object[instanceKey] = this.getValueFromSerializer(property, json); } } }); return object; } getInstanceKey(fields, jsonKey) { jsonKey = this.cleanString(jsonKey); let instanceKey = Strings_1.Strings.Empty; fields.forEach(element => { if (this.cleanString(element) === jsonKey) { instanceKey = element; } }); return instanceKey; } cleanString(stringToClean) { return stringToClean.replace('field_', Strings_1.Strings.Empty) .replace(/[^a-zA-Z0-9]/g, Strings_1.Strings.Empty) .trim() .toLowerCase(); } propertyIsSimple(property) { return Array.isArray(property) && typeof property[0] !== 'object' || typeof property !== 'object'; } propertyIsArrayOfObjects(property) { return Array.isArray(property) && typeof property[0] === 'object'; } getValueFromSerializer(property, json) { const newSerializer = new JsonSerializer(); try { return newSerializer.Deserialize(property.constructor, json); } catch (error) { return new property.constructor(); } } getArrayValuesFromSerializer(property, json) { const values = []; if (property) { for (const element of json) { values.push(this.getValueFromSerializer(property, element)); } } return values; } serializeSimpleField(jsonElement, object, instanceKey) { if (!Array.isArray(jsonElement)) { object[instanceKey] = jsonElement; } else { if (jsonElement.length === 1) { object[instanceKey] = typeof jsonElement[0] === 'object' ? this.getSingleValue(jsonElement[0]) : [jsonElement[0]]; } else if (jsonElement.length > 1) { object[instanceKey] = this.getArrayValue(jsonElement); } } } getSingleValue(object) { let value = null; keysToCheck.forEach(element => { if (object && object[element]) { value = object[element]; } }); return value; } getArrayValue(array) { const values = new Array(); array.forEach(element => { if (typeof element !== 'object') { values.push(element); } else { values.push(this.getSingleValue(element)); } }); return values; } } exports.JsonSerializer = JsonSerializer; //# sourceMappingURL=JsonSerializer.js.map