UNPKG

@asposecloud/aspose-tasks-cloud

Version:
178 lines (174 loc) 6.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* * MIT License * Copyright (c) 2019 Aspose Pty Ltd * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ const model_1 = require("../model/model"); const primitives = [ "string", "boolean", "double", "integer", "long", "float", "number", "any", ]; /** * Serialisation helper. */ class ObjectSerializer { /** * Serilize object to json string. */ static serialize(data, type, isRecursiveCall = false) { if (data === undefined) { return data; } else if (primitives.indexOf(type.toLowerCase()) !== -1) { return data; } else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6 let subType = type.replace("Array<", ""); // Array<Type> => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type const transformedData = []; for (const index in data) { if (data.hasOwnProperty(index)) { const date = data[index]; transformedData.push(ObjectSerializer.serialize(date, subType, true)); } } return transformedData; } else if (type === "Date") { const result = data.toISOString(); return isRecursiveCall ? result : "\"" + result + "\""; ; } else { if (model_1.enumsMap[type]) { return data; } if (!model_1.typeMap[type]) { // in case we dont know the type return data; } // get the map for the correct type. const attributeTypes = model_1.typeMap[type].getAttributeTypeMap(); const instance = {}; for (const index in attributeTypes) { if (attributeTypes.hasOwnProperty(index)) { const attributeType = attributeTypes[index]; instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type, true); } } return instance; } } /** * Deserialize object from json string */ static deserialize(data, type) { // polymorphism may change the actual type. type = ObjectSerializer.findCorrectType(data, type); if (data === undefined || data === null) { return data; } else if (primitives.indexOf(type.toLowerCase()) !== -1) { return data; } else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6 let subType = type.replace("Array<", ""); // Array<Type> => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type const transformedData = []; for (const index in data) { if (data.hasOwnProperty(index)) { const date = data[index]; transformedData.push(ObjectSerializer.deserialize(date, subType)); } } return transformedData; } else if (type === "Date") { return new Date(data); } else { if (model_1.enumsMap[type]) { // is Enum return data; } if (!model_1.typeMap[type]) { // dont know the type return data; } const instance = new model_1.typeMap[type](); const attributeTypes = model_1.typeMap[type].getAttributeTypeMap(); for (const index in attributeTypes) { if (attributeTypes.hasOwnProperty(index)) { const attributeType = attributeTypes[index]; const baseName = this.GetPropertyCaseInsensitive(data, attributeType.baseName); if (baseName != null) { instance[attributeType.name] = ObjectSerializer.deserialize(data[baseName], attributeType.type); } } } return instance; } } static GetPropertyCaseInsensitive(obj, property) { let props = []; for (var i in obj) { if (obj.hasOwnProperty(i)) { props.push(i); } } let prop; while (prop = props.pop()) { if (prop.toLowerCase() === property.toLowerCase()) { return prop; } } return null; } static findCorrectType(data, expectedType) { if (data === undefined) { return expectedType; } else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) { return expectedType; } else if (expectedType === "Date") { return expectedType; } else { if (model_1.enumsMap[expectedType]) { return expectedType; } if (!model_1.typeMap[expectedType]) { return expectedType; // w/e we don't know the type } const discriminatorProperty = model_1.typeMap[expectedType].discriminator; if (discriminatorProperty != null && data[discriminatorProperty]) { return data[discriminatorProperty]; // use the type given in the discriminator } else { return expectedType; // discriminator was not present (or an empty string) } } } } exports.ObjectSerializer = ObjectSerializer; //# sourceMappingURL=objectSerializer.js.map