UNPKG

turbocommons-ts

Version:

General purpose library that implements frequently used and generic software development tasks

1 lines 2.17 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.SerializationManager=void 0;var JavaPropertiesObject_1=require("../model/JavaPropertiesObject"),ArrayUtils_1=require("../utils/ArrayUtils"),ObjectUtils_1=require("../utils/ObjectUtils"),SerializationManager=function(){function t(){this.strictMode=!0}return t.prototype.classToJson=function(t){return JSON.stringify(t)},t.prototype.classToObject=function(){},t.prototype.javaPropertiesObjectToString=function(t){return t.toString()},t.prototype.jsonToClass=function(t,e){return this.objectToClass(JSON.parse(t),e)},t.prototype.objectToClass=function(t,e){var r=ObjectUtils_1.ObjectUtils.getKeys(t),o=e.constructor.name,i=ObjectUtils_1.ObjectUtils.getKeys(e);if(this.strictMode&&r.length!==i.length)throw new Error("(strict mode): ["+r.join(",")+"] keys do not match "+o+" props: ["+i.join(",")+"]");for(var s=0,n=r;s<n.length;s++){var a=n[s];if(e.hasOwnProperty(a)){var c=t[a];if(null!==c){if(null!==e[a]&&void 0!==e[a]){var l="<"+o+"."+a+"> was "+typeof c+" but expected to be ";if(ArrayUtils_1.ArrayUtils.isArray(e[a])){if(!ArrayUtils_1.ArrayUtils.isArray(c))throw new Error(l+"array");if(e[a].length>0){if(1!==e[a].length)throw new Error("To define a typed list, <"+o+"."+a+"> must contain only 1 default typed element");var f=e[a][0],p=ObjectUtils_1.ObjectUtils.isObject(f)&&"Object"!==f.constructor.name;e[a]=[];for(var u=0,j=c;u<j.length;u++){var b=j[u];if(p)e[a].push(this.objectToClass(b,ObjectUtils_1.ObjectUtils.clone(f)));else{if(typeof b!=typeof f)throw new Error("<"+o+"."+a+"> is defined as array of "+typeof f+" but received "+typeof b);e[a].push(b)}}continue}}if(ObjectUtils_1.ObjectUtils.isObject(e[a])){if(!ObjectUtils_1.ObjectUtils.isObject(c))throw new Error(l+e[a].constructor.name);"Object"!==e[a].constructor.name&&(c=this.objectToClass(c,e[a]))}if(typeof e[a]!=typeof c)throw new Error(l+typeof e[a])}e[a]=c}}else if(this.strictMode)throw new Error("(strict mode): <"+a+"> not found in "+o)}return e},t.prototype.stringToJavaPropertiesObject=function(t){return new JavaPropertiesObject_1.JavaPropertiesObject(t)},t}();exports.SerializationManager=SerializationManager;