asposeslidescloud
Version:
Aspose.Slides Cloud SDK for Node.js
186 lines (181 loc) • 6.75 kB
JavaScript
"use strict";
/*
* MIT License
* Copyright (c) 2017 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectSerializer = void 0;
//import { enumsMap, typeMap } from "../model/model";
const enumsMap = {};
const typeMap = {};
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<", ""); // 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));
}
}
return transformedData;
}
else if (type === "Date") {
return data.toString();
}
else {
if (enumsMap[type]) {
return data;
}
if (!typeMap[type]) { // in case we dont know the type
return data;
}
// get the map for the correct type.
const attributeTypes = 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);
}
}
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 (enumsMap[type]) { // is Enum
return data;
}
if (!typeMap[type]) { // dont know the type
if (type == "Buffer") {
return data;
}
if (typeof data == "string") {
return JSON.parse(data);
}
if (Buffer.isBuffer(data)) {
return JSON.parse(data.toString());
}
return data;
}
const instance = new typeMap[type]();
const attributeTypes = 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;
}
}
/**
* cast object to string
*/
static toString(data) {
if (data === undefined)
return "";
return String(data);
}
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 (enumsMap[expectedType]) {
return expectedType;
}
if (!typeMap[expectedType]) {
return expectedType; // w/e we don't know the type
}
// Check the discriminator
const discriminatorProperty = 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;