UNPKG

asposecellscloud

Version:

Aspose.Cells Cloud for Node.js to create, repair, merge, parse and convert excel files. Convert excel to PDF, JSON, XML, TSV, HTML and so on.

170 lines (169 loc) 6.43 kB
"use strict"; /* * MIT License * * Copyright (c) 2025 Aspose.Cells Cloud * 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. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectSerializer = void 0; 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) { 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<", ""); subType = subType.substring(0, subType.length - 1); const transformedData = []; for (const index in data) { if (data.hasOwnProperty(index)) { const date = data[index]; transformedData.push(ObjectSerializer.serialize(date, subType)); } } return transformedData; } else if (type === "Date") { return data.toString(); } 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. var attributeTypes = model_1.typeMap[type].getAttributeTypeMap(); if (attributeTypes.length == 0 && data != null) { attributeTypes = model_1.typeMap[data.constructor.name].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); } } 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]; if (data.hasOwnProperty(attributeType.baseName)) { instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); } } } return instance; } } 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 } // Check the discriminator const discriminatorProperty = model_1.typeMap[expectedType].discriminator; if (discriminatorProperty == null) { return expectedType; // the type does not have a discriminator. use it. } else { if (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;