Socket
Socket
Sign inDemoInstall

typescript-json-serializer

Package Overview
Dependencies
2
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.1.0 to 6.0.0

16

CHANGELOG.md

@@ -0,1 +1,17 @@

<a name="6.0.0"></a>
# 6.0.0 (2023-03-29)
Check the new [README](./README.md) to understand how to use `typescript-json-serializer` now.
### BREAKING CHANGES
- `JsonObject`: empty object `{}` is no longer passed to the constructor on deserialize. This list of default params must be pass to the `JsonObject` thanks to the new `constructorParams` property of the `JsonObjectOptions` if the class contains a constructor with params used in the constructor body.
### Fixes
- **deps:** bump json5 from 2.2.0 to 2.2.3 ([#196](https://github.com/GillianPerard/typescript-json-serializer/pull/196)) ([63d941d](https://github.com/GillianPerard/typescript-json-serializer/commit/63d941dcf2e77cd8749471a03bea097bd773acd7))
- **JsonObject**: `JsonObjectOptions` has been added to manage default constructor params values ([#200](https://github.com/GillianPerard/typescript-json-serializer/issues/200)) ([8f9eba8](https://github.com/GillianPerard/typescript-json-serializer/commit/8f9eba8a9b74beff602a7e30fe28b2c936c4643f))
- **JsonSerializer**: take in account the default value of properties when it checks if the value is `required` ([#201](https://github.com/GillianPerard/typescript-json-serializer/issues/201)) ([136ed86](https://github.com/GillianPerard/typescript-json-serializer/commit/136ed8685c2d94a89acd4b7f76fcf6a80fdea179))
<a name="5.1.0"></a>

@@ -2,0 +18,0 @@ # 5.1.0 (2022-11-01)

2

dist/index.cjs.js

@@ -1,1 +0,1 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib");require("reflect-metadata");var t=function(){function e(){}return e.getBaseClass=function(e){return e?Reflect.getPrototypeOf(e):void 0},e.getJsonPropertiesMetadata=function(t,r){if(t){var i=""+e.apiMap+(r||t.constructor.name);return Reflect.getMetadata(i,t)}},e.getParamTypes=function(t){return t?Reflect.getMetadata(e.designParamTypes,t):void 0},e.getJsonObjectMetadata=function(t){return t?Reflect.getMetadata(e.apiMapJsonObject,t):void 0},e.getType=function(t,r){return t?Reflect.getMetadata(e.designType,t,r):void 0},e.isJsonObject=function(t){return!!t&&Reflect.hasOwnMetadata(e.apiMapJsonObject,t)},e.setJsonPropertiesMetadata=function(t,r){if(r){var i=""+e.apiMap+r.constructor.name;Reflect.defineMetadata(i,t,r)}},e.setJsonObject=function(t,r){r&&Reflect.defineMetadata(e.apiMapJsonObject,t,r)},e.setType=function(t,r,i){r&&t&&Reflect.defineMetadata(e.designType,t,r,i)},e.apiMap="api:map:",e.apiMapJsonObject=e.apiMap+"jsonObject",e.designType="design:type",e.designParamTypes="design:paramtypes",e}(),r=function(e){return"string"==typeof e},i=function(e){return null!==e&&"object"==typeof e&&!o(e)},o=function(e){return Array.isArray(e)},n=function(e){return"[object Date]"===toString.call(e)},a=function(e){return t.isJsonObject(e)},s=function(e){return[null,void 0].includes(e)},l=function(e){return e instanceof Set},u=function(e){try{var t=JSON.parse(e);return"object"==typeof t?t:e}catch(t){return e}},c=function(e,t){return e.filter((function(e){return!t.some((function(t){return e===t}))}))},p=function(){this.errorCallback=d,this.nullishPolicy={undefined:"remove",null:"allow"},this.additionalPropertiesPolicy="remove"},d=function(e){console.error(e)},f=function(){function d(t){this.options=new p,this.options=e.__assign(e.__assign({},this.options),t)}return d.prototype.deserialize=function(e,t){return r(e)&&(e=u(e)),o(e)?this.deserializeObjectArray(e,t):i(e)?this.deserializeObject(e,t):void this.error("Fail to deserialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},d.prototype.deserializeObject=function(e,t){var o=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Object."),null;if(void 0!==e){if(r(e)&&(e=u(e)),i(e)){var n=function(e){if("function"!=typeof e)return!1;try{Reflect.construct(String,[],e)}catch(e){return!1}return!0}(t)?new t({}):t,a=this.getJsonPropertiesMetadata(n);if(!a)return n;var s=Object.keys(a);if(s.forEach((function(t){var r=a[t],i=o.deserializeProperty(n,t,e,r);o.checkRequiredProperty(r,n,t,i,e,!1),o.isAllowedProperty(t,i)&&(n[t]=i)})),"remove"===this.options.additionalPropertiesPolicy)return n;var l=c(Object.keys(e),s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return n[t]=e[t]})),n):n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Object'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Object.")},d.prototype.deserializeObjectArray=function(e,t){var i=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Array."),null;if(void 0!==e){if(r(e)&&(e=u(e)),o(e))return e.reduce((function(e,r){var o=i.deserializeObject(r,t);return(!s(o)||null===o&&"remove"!==i.options.nullishPolicy.null||void 0===o&&"remove"!==i.options.nullishPolicy.undefined)&&e.push(o),e}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Array.")},d.prototype.serialize=function(e){return o(e)?this.serializeObjectArray(e):i(e)?this.serializeObject(e):void this.error("Fail to serialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},d.prototype.serializeObject=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Object."),null;if(void 0!==e){if(!i(e))return e;var r=this.getJsonPropertiesMetadata(e);if(!r)return e;var n={},a=Object.keys(e),s=Object.keys(r);if(s.forEach((function(i){var s=r[i];if(a.includes(i)){var l=void 0;s.beforeSerialize&&(l=e[i],e[i]=s.beforeSerialize(e[i],e));var u=t.serializeProperty(e,i,s);if(s.afterSerialize&&(u=s.afterSerialize(u,e)),e[i]=l||e[i],o(s.name))s.name.forEach((function(e){t.isAllowedProperty(e,u[e])&&(n[e]=u[e])}));else if(t.checkRequiredProperty(s,e,i,u,e),t.isAllowedProperty(i,u))if(s.isNameOverridden||void 0===t.options.formatPropertyName)n[s.name]=u;else{var c=t.options.formatPropertyName(s.name);n[c]=u}}else o(s.name)?s.name.forEach((function(e){t.isAllowedProperty(e,void 0)&&(n[e]=void 0)})):(t.checkRequiredProperty(s,e,i,void 0,e),t.isAllowedProperty(i,void 0)&&(n[s.name]=void 0))})),"remove"===this.options.additionalPropertiesPolicy)return n;var l=c(a,s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return n[t]=e[t]})),n):n}"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Object.")},d.prototype.serializeObjectArray=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Array."),null;if(void 0!==e){if(o(e))return e.reduce((function(e,r){var i=t.serializeObject(r);return(!s(i)||null===i&&"remove"!==t.options.nullishPolicy.null||void 0===i&&"remove"!==t.options.nullishPolicy.undefined)&&e.push(i),e}),[]);this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e)+".")}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Array.")},d.prototype.checkRequiredProperty=function(e,t,r,i,o,n){if(void 0===n&&(n=!0),e.required&&s(i)){var a=t.constructor.name;this.error("Fail to "+(n?"serialize":"deserialize")+": Property '"+r+"' is required in "+a+" "+JSON.stringify(o)+".")}},d.prototype.deserializeProperty=function(e,r,i,o){if(!s(i)){var n=this.getDataSource(i,o,this.options.formatPropertyName);if(s(n))return n;var l,u=t.getType(e,r),c=this.getDataStructureInformation(u,e[r],o),p=c.isArrayProperty,d=c.isSetProperty,f=c.isMapProperty,y=c.isDictionaryProperty,h=o.type||u;o.beforeDeserialize&&(n=o.beforeDeserialize(n,e));var v=o.predicate;return y||f?(l=this.deserializeDictionary(n,h,v),f&&(l=new Map(Object.entries(l)))):p||d?(l=this.deserializeArray(n,h,v),d&&(l=new Set(l))):!a(h)&&!v||v&&!v(n,i)?l=this.deserializePrimitive(n,h.name):(h=o.predicate?o.predicate(n,i):h,l=this.deserializeObject(n,h)),o.afterDeserialize&&(l=o.afterDeserialize(l,e)),l}},d.prototype.deserializePrimitive=function(e,t){if(s(t))return e;if(typeof e===(t=t.toLowerCase()))return e;var r="Fail to deserialize: type '"+typeof e+"' is not assignable to type '"+t+"'.\nReceived: "+JSON.stringify(e);switch(t){case"string":var i=e.toString();return"[object Object]"===i?void this.error(r):i;case"number":return function(e){return"number"==typeof e}(e)?+e:void this.error(r);case"boolean":return void this.error(r);case"date":return function(e){return!n(e)&&!o(e)&&!isNaN(Date.parse(e))}(e)?new Date(e):void this.error(r);default:return e}},d.prototype.deserializeDictionary=function(e,t,r){var o=this;if(i(e)){var n={};return Object.keys(e).forEach((function(i){var s=r?r(e[i],e):void 0;a(t)||s?n[i]=o.deserializeObject(e[i],s||t):n[i]=o.deserializePrimitive(e[i],typeof e[i])})),n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},d.prototype.deserializeArray=function(e,t,r){var i=this;if(o(e))return e.reduce((function(o,n){var l;return a(t)||r?(t=r?r(n,e):t,l=i.deserializeObject(n,t)):l=i.deserializePrimitive(n,typeof n),(!s(l)||null===l&&"remove"!==i.options.nullishPolicy.null||void 0===l&&"remove"!==i.options.nullishPolicy.undefined)&&o.push(l),o}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))},d.prototype.error=function(e){this.options.errorCallback&&this.options.errorCallback(e)},d.prototype.getClassesJsonPropertiesMetadata=function(e,r){return e?e.reduce((function(e,i){var o=t.getJsonPropertiesMetadata(r,i);return o&&e.push(o),e}),[]):[]},d.prototype.getDataSource=function(e,t,r){var i=t.name,n=t.isNameOverridden;if(o(i)){var a={};return i.forEach((function(t){return a[t]=e[t]})),a}return!n&&r?(i=r(i),e[i]):e[i]},d.prototype.getDataStructureInformation=function(e,t,r){var i,n,a,s,u;if(r.dataStructure)return{isArrayProperty:null!==(i="array"===r.dataStructure)&&void 0!==i&&i,isDictionaryProperty:null!==(n="dictionary"===r.dataStructure)&&void 0!==n&&n,isMapProperty:null!==(a="map"===r.dataStructure)&&void 0!==a&&a,isSetProperty:null!==(s="set"===r.dataStructure)&&void 0!==s&&s};var c,p=null===(u=null==e?void 0:e.name)||void 0===u?void 0:u.toLowerCase();return"object"===p?{isArrayProperty:o(t),isDictionaryProperty:!1,isMapProperty:(c=t,c instanceof Map),isSetProperty:l(t)}:{isArrayProperty:"array"===p,isDictionaryProperty:!1,isMapProperty:"map"===p,isSetProperty:"set"===p}},d.prototype.getJsonPropertiesMetadata=function(r){var i,o=(null!==(i=t.getJsonObjectMetadata(r.constructor))&&void 0!==i?i:{}).baseClassNames,n=t.getJsonPropertiesMetadata(r);if(!(n||o&&o.length))return n;if(o&&o.length){var a=this.getClassesJsonPropertiesMetadata(o,r);return this.mergeJsonPropertiesMetadata.apply(this,e.__spreadArray(e.__spreadArray([],a),[n]))}return n},d.prototype.isAllowedProperty=function(e,t){if(s(t)){if("disallow"===this.options.nullishPolicy[""+t])return this.error("Disallowed "+t+" value detected: "+e+"."),!1;if("remove"===this.options.nullishPolicy[""+t])return!1}return!0},d.prototype.mergeJsonPropertiesMetadata=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];var i={};return t.forEach((function(t){t&&Object.keys(t).forEach((function(r){i[r]=e.__assign(e.__assign({},i[r]),t[r])}))})),i},d.prototype.serializeDictionary=function(e){var t=this;if(i(e)){var r={};return Object.keys(e).forEach((function(i){r[i]=t.serializeObject(e[i])})),r}this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},d.prototype.serializeProperty=function(e,i,o){var s,l=this,u=e[i],c=t.getType(e,i),p=this.getDataStructureInformation(c,u,o),d=p.isArrayProperty,f=p.isDictionaryProperty,y=p.isMapProperty,h=p.isSetProperty,v=o.predicate,g=o.type||c,P=a(g);if(u&&(P||v)){if(d||h){var b=h?Array.from(u):u;return this.serializeObjectArray(b)}if(f||y){if(!y)return this.serializeDictionary(u);var m={};return u.forEach((function(e,t){r(t)?m[t]=e:l.error("Fail to serialize: type of '"+typeof t+"' is not assignable to type 'string'.\nReceived: "+JSON.stringify(t)+".")})),this.serializeDictionary(m)}return this.serializeObject(u)}return"date"===(null===(s=null==g?void 0:g.name)||void 0===s?void 0:s.toLocaleLowerCase())&&n(u)?u.toISOString():u},d}(),y=function(r){var i=t.getBaseClass(r);return i&&i.name?e.__spreadArray(e.__spreadArray([],y(i)),[i.name]):[]},h=function(e){var t,r=e.toString().split("}")[0].replace(/(\/\*[\s\S]*?\*\/|\/\/.*$)/gm,"").replace(/[\r\t\n\v\f ]/g,""),i=r.length;","===r[i-2]&&(t=r[i-1]);var o=t?new RegExp("(?:(this|"+t+"|\\("+t+"=t.call\\(this(,.)*\\)\\))\\.)([^,;\n}]+)","gm"):new RegExp("(?:(this)\\.)([^,;\n}]+)","gm"),n=new Map,a=/(?:.*(?:constructor|function).*?(?=\())(?:\()(.+?(?=\)))/m.exec(r);if(!a||!a.length)return n;for(var s,l=a[1].split(","),u=function(){var e=s.length-1,t=s[e].split("="),r=l.findIndex((function(e){return e===t[1]}));r>-1&&n.set(r,t[0])};s=o.exec(r);)u();return n},v=function(o,n){var a={name:o.toString()};return n?r(n)?(a.name=n,a.isNameOverridden=!0,a):(i(n)&&(a=e.__assign(e.__assign({},a),n),n.name&&(a.name=n.name,a.isNameOverridden=!0),function(e){if(!e)return!1;var r=t.getParamTypes(e),i=e.length;return(1===i||2===i)&&!r}(n.type)&&(delete a.type,a.predicate=n.type)),a):a};exports.JsonObject=function(){return function(e){var r=y(e);t.setJsonObject({baseClassNames:r},e)}},exports.JsonProperty=function(e){return function(r,i,o){var n;if(void 0===i&&r.prototype){var a=t.getParamTypes(r)[o];i=h(r.prototype.constructor).get(o),r=r.prototype,t.setType(a,r,i)}var s=null!==(n=t.getJsonPropertiesMetadata(r))&&void 0!==n?n:{};s[i]=v(i,e),t.setJsonPropertiesMetadata(s,r)}},exports.JsonSerializer=f,exports.JsonSerializerOptions=p,exports.isNullish=s,exports.logError=d,exports.throwError=function(e){throw new Error(e)};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib");require("reflect-metadata");var t=function(){function e(){}return e.getBaseClass=function(e){return e?Reflect.getPrototypeOf(e):void 0},e.getJsonPropertiesMetadata=function(t,r){if(t){var i=""+e.apiMap+(r||t.constructor.name);return Reflect.getMetadata(i,t)}},e.getParamTypes=function(t){return t?Reflect.getMetadata(e.designParamTypes,t):void 0},e.getJsonObjectMetadata=function(t){return t?Reflect.getMetadata(e.apiMapJsonObject,t):void 0},e.getType=function(t,r){return t?Reflect.getMetadata(e.designType,t,r):void 0},e.isJsonObject=function(t){return!!t&&Reflect.hasOwnMetadata(e.apiMapJsonObject,t)},e.setJsonPropertiesMetadata=function(t,r){if(r){var i=""+e.apiMap+r.constructor.name;Reflect.defineMetadata(i,t,r)}},e.setJsonObject=function(t,r){r&&Reflect.defineMetadata(e.apiMapJsonObject,t,r)},e.setType=function(t,r,i){r&&t&&Reflect.defineMetadata(e.designType,t,r,i)},e.apiMap="api:map:",e.apiMapJsonObject=e.apiMap+"jsonObject",e.designType="design:type",e.designParamTypes="design:paramtypes",e}(),r=function(e){return"string"==typeof e},i=function(e){return null!==e&&"object"==typeof e&&!o(e)},o=function(e){return Array.isArray(e)},n=function(e){return"[object Date]"===toString.call(e)},a=function(e){return t.isJsonObject(e)},s=function(e){return[null,void 0].includes(e)},l=function(e){return e instanceof Set},u=function(e){try{var t=JSON.parse(e);return"object"==typeof t?t:e}catch(t){return e}},c=function(e,t){return e.filter((function(e){return!t.some((function(t){return e===t}))}))},p=function(){this.errorCallback=d,this.nullishPolicy={undefined:"remove",null:"allow"},this.additionalPropertiesPolicy="remove"},d=function(e){console.error(e)},f=function(){function d(t){this.options=new p,this.options=e.__assign(e.__assign({},this.options),t)}return d.prototype.deserialize=function(e,t){return r(e)&&(e=u(e)),o(e)?this.deserializeObjectArray(e,t):i(e)?this.deserializeObject(e,t):void this.error("Fail to deserialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},d.prototype.deserializeObject=function(o,n){var a,l=this;if(null===o)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Object."),null;if(void 0!==o){if(r(o)&&(o=u(o)),i(o)){var p;if(function(e){if("function"!=typeof e)return!1;try{Reflect.construct(String,[],e)}catch(e){return!1}return!0}(n)){var d=Object.create(n.prototype),f=t.getJsonObjectMetadata(d.constructor),y=null!==(a=null==f?void 0:f.constructorParams)&&void 0!==a?a:[];p=new(n.bind.apply(n,e.__spreadArray([void 0],y)))}else p=n;var v=this.getJsonPropertiesMetadata(p);if(!v)return p;var h=Object.keys(v);if(h.forEach((function(e){var t=v[e],r=l.deserializeProperty(p,e,o,t);l.checkRequiredProperty(t,p,e,r,o,!1);var i=p[e];r===i||(null!==r||void 0!==i)&&s(r)||(i=r),l.isAllowedProperty(e,i)&&(p[e]=i)})),"remove"===this.options.additionalPropertiesPolicy)return p;var g=c(Object.keys(o),h);return g.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(o)+": "+g+"."):"allow"===this.options.additionalPropertiesPolicy&&g.forEach((function(e){return p[e]=o[e]})),p):p}this.error("Fail to deserialize: type '"+typeof o+"' is not assignable to type 'Object'.\nReceived: "+JSON.stringify(o))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Object.")},d.prototype.deserializeObjectArray=function(e,t){var i=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Array."),null;if(void 0!==e){if(r(e)&&(e=u(e)),o(e))return e.reduce((function(e,r){var o=i.deserializeObject(r,t);return(!s(o)||null===o&&"remove"!==i.options.nullishPolicy.null||void 0===o&&"remove"!==i.options.nullishPolicy.undefined)&&e.push(o),e}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Array.")},d.prototype.serialize=function(e){return o(e)?this.serializeObjectArray(e):i(e)?this.serializeObject(e):void this.error("Fail to serialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},d.prototype.serializeObject=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Object."),null;if(void 0!==e){if(!i(e))return e;var r=this.getJsonPropertiesMetadata(e);if(!r)return e;var n={},a=Object.keys(e),s=Object.keys(r);if(s.forEach((function(i){var s=r[i];if(a.includes(i)){var l=void 0;s.beforeSerialize&&(l=e[i],e[i]=s.beforeSerialize(e[i],e));var u=t.serializeProperty(e,i,s);if(s.afterSerialize&&(u=s.afterSerialize(u,e)),e[i]=l||e[i],o(s.name))s.name.forEach((function(e){t.isAllowedProperty(e,u[e])&&(n[e]=u[e])}));else if(t.checkRequiredProperty(s,e,i,u,e),t.isAllowedProperty(i,u))if(s.isNameOverridden||void 0===t.options.formatPropertyName)n[s.name]=u;else{var c=t.options.formatPropertyName(s.name);n[c]=u}}else o(s.name)?s.name.forEach((function(e){t.isAllowedProperty(e,void 0)&&(n[e]=void 0)})):(t.checkRequiredProperty(s,e,i,void 0,e),t.isAllowedProperty(i,void 0)&&(n[s.name]=void 0))})),"remove"===this.options.additionalPropertiesPolicy)return n;var l=c(a,s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return n[t]=e[t]})),n):n}"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Object.")},d.prototype.serializeObjectArray=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Array."),null;if(void 0!==e){if(o(e))return e.reduce((function(e,r){var i=t.serializeObject(r);return(!s(i)||null===i&&"remove"!==t.options.nullishPolicy.null||void 0===i&&"remove"!==t.options.nullishPolicy.undefined)&&e.push(i),e}),[]);this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e)+".")}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Array.")},d.prototype.checkRequiredProperty=function(e,t,r,i,o,n){if(void 0===n&&(n=!0),e.required&&s(i)&&s(t[r])){var a=t.constructor.name;this.error("Fail to "+(n?"serialize":"deserialize")+": Property '"+r+"' is required in "+a+" "+JSON.stringify(o)+".")}},d.prototype.deserializeProperty=function(e,r,i,o){if(!s(i)){var n=this.getDataSource(i,o,this.options.formatPropertyName);if(s(n))return n;var l,u=t.getType(e,r),c=this.getDataStructureInformation(u,e[r],o),p=c.isArrayProperty,d=c.isSetProperty,f=c.isMapProperty,y=c.isDictionaryProperty,v=o.type||u;o.beforeDeserialize&&(n=o.beforeDeserialize(n,e));var h=o.predicate;return y||f?(l=this.deserializeDictionary(n,v,h),f&&(l=new Map(Object.entries(l)))):p||d?(l=this.deserializeArray(n,v,h),d&&(l=new Set(l))):!a(v)&&!h||h&&!h(n,i)?l=this.deserializePrimitive(n,v.name):(v=o.predicate?o.predicate(n,i):v,l=this.deserializeObject(n,v)),o.afterDeserialize&&(l=o.afterDeserialize(l,e)),l}},d.prototype.deserializePrimitive=function(e,t){if(s(t))return e;if(typeof e===(t=t.toLowerCase()))return e;var r="Fail to deserialize: type '"+typeof e+"' is not assignable to type '"+t+"'.\nReceived: "+JSON.stringify(e);switch(t){case"string":var i=e.toString();return"[object Object]"===i?void this.error(r):i;case"number":return function(e){return"number"==typeof e}(e)?+e:void this.error(r);case"boolean":return void this.error(r);case"date":return function(e){return!n(e)&&!o(e)&&!isNaN(Date.parse(e))}(e)?new Date(e):void this.error(r);default:return e}},d.prototype.deserializeDictionary=function(e,t,r){var o=this;if(i(e)){var n={};return Object.keys(e).forEach((function(i){var s=r?r(e[i],e):void 0;a(t)||s?n[i]=o.deserializeObject(e[i],s||t):n[i]=o.deserializePrimitive(e[i],typeof e[i])})),n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},d.prototype.deserializeArray=function(e,t,r){var i=this;if(o(e))return e.reduce((function(o,n){var l;return a(t)||r?(t=r?r(n,e):t,l=i.deserializeObject(n,t)):l=i.deserializePrimitive(n,typeof n),(!s(l)||null===l&&"remove"!==i.options.nullishPolicy.null||void 0===l&&"remove"!==i.options.nullishPolicy.undefined)&&o.push(l),o}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))},d.prototype.error=function(e){this.options.errorCallback&&this.options.errorCallback(e)},d.prototype.getClassesJsonPropertiesMetadata=function(e,r){return e?e.reduce((function(e,i){var o=t.getJsonPropertiesMetadata(r,i);return o&&e.push(o),e}),[]):[]},d.prototype.getDataSource=function(e,t,r){var i=t.name,n=t.isNameOverridden;if(o(i)){var a={};return i.forEach((function(t){return a[t]=e[t]})),a}return!n&&r?(i=r(i),e[i]):e[i]},d.prototype.getDataStructureInformation=function(e,t,r){var i,n,a,s,u;if(r.dataStructure)return{isArrayProperty:null!==(i="array"===r.dataStructure)&&void 0!==i&&i,isDictionaryProperty:null!==(n="dictionary"===r.dataStructure)&&void 0!==n&&n,isMapProperty:null!==(a="map"===r.dataStructure)&&void 0!==a&&a,isSetProperty:null!==(s="set"===r.dataStructure)&&void 0!==s&&s};var c,p=null===(u=null==e?void 0:e.name)||void 0===u?void 0:u.toLowerCase();return"object"===p?{isArrayProperty:o(t),isDictionaryProperty:!1,isMapProperty:(c=t,c instanceof Map),isSetProperty:l(t)}:{isArrayProperty:"array"===p,isDictionaryProperty:!1,isMapProperty:"map"===p,isSetProperty:"set"===p}},d.prototype.getJsonPropertiesMetadata=function(r){var i,o=(null!==(i=t.getJsonObjectMetadata(r.constructor))&&void 0!==i?i:{}).baseClassNames,n=t.getJsonPropertiesMetadata(r);if(!(n||o&&o.length))return n;if(o&&o.length){var a=this.getClassesJsonPropertiesMetadata(o,r);return this.mergeJsonPropertiesMetadata.apply(this,e.__spreadArray(e.__spreadArray([],a),[n]))}return n},d.prototype.isAllowedProperty=function(e,t){if(s(t)){if("disallow"===this.options.nullishPolicy[""+t])return this.error("Disallowed "+t+" value detected: "+e+"."),!1;if("remove"===this.options.nullishPolicy[""+t])return!1}return!0},d.prototype.mergeJsonPropertiesMetadata=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];var i={};return t.forEach((function(t){t&&Object.keys(t).forEach((function(r){i[r]=e.__assign(e.__assign({},i[r]),t[r])}))})),i},d.prototype.serializeDictionary=function(e){var t=this;if(i(e)){var r={};return Object.keys(e).forEach((function(i){r[i]=t.serializeObject(e[i])})),r}this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},d.prototype.serializeProperty=function(e,i,o){var s,l=this,u=e[i],c=t.getType(e,i),p=this.getDataStructureInformation(c,u,o),d=p.isArrayProperty,f=p.isDictionaryProperty,y=p.isMapProperty,v=p.isSetProperty,h=o.predicate,g=o.type||c,P=a(g);if(u&&(P||h)){if(d||v){var b=v?Array.from(u):u;return this.serializeObjectArray(b)}if(f||y){if(!y)return this.serializeDictionary(u);var m={};return u.forEach((function(e,t){r(t)?m[t]=e:l.error("Fail to serialize: type of '"+typeof t+"' is not assignable to type 'string'.\nReceived: "+JSON.stringify(t)+".")})),this.serializeDictionary(m)}return this.serializeObject(u)}return"date"===(null===(s=null==g?void 0:g.name)||void 0===s?void 0:s.toLocaleLowerCase())&&n(u)?u.toISOString():u},d}(),y=function(r){var i=t.getBaseClass(r);return i&&i.name?e.__spreadArray(e.__spreadArray([],y(i)),[i.name]):[]},v=function(e){var t,r=e.toString().split("}")[0].replace(/(\/\*[\s\S]*?\*\/|\/\/.*$)/gm,"").replace(/[\r\t\n\v\f ]/g,""),i=r.length;","===r[i-2]&&(t=r[i-1]);var o=t?new RegExp("(?:(this|"+t+"|\\("+t+"=t.call\\(this(,.)*\\)\\))\\.)([^,;\n}]+)","gm"):new RegExp("(?:(this)\\.)([^,;\n}]+)","gm"),n=new Map,a=/(?:.*(?:constructor|function).*?(?=\())(?:\()(.+?(?=\)))/m.exec(r);if(!a||!a.length)return n;for(var s,l=a[1].split(","),u=function(){var e=s.length-1,t=s[e].split("="),r=l.findIndex((function(e){return e===t[1]}));r>-1&&n.set(r,t[0])};s=o.exec(r);)u();return n},h=function(o,n){var a={name:o.toString()};return n?r(n)?(a.name=n,a.isNameOverridden=!0,a):(i(n)&&(a=e.__assign(e.__assign({},a),n),n.name&&(a.name=n.name,a.isNameOverridden=!0),function(e){if(!e)return!1;var r=t.getParamTypes(e),i=e.length;return(1===i||2===i)&&!r}(n.type)&&(delete a.type,a.predicate=n.type)),a):a};exports.JsonObject=function(e){return function(r){var i,o=y(r),n=null!==(i=null==e?void 0:e.constructorParams)&&void 0!==i?i:[];t.setJsonObject({baseClassNames:o,constructorParams:n},r)}},exports.JsonProperty=function(e){return function(r,i,o){var n;if(void 0===i&&r.prototype){var a=t.getParamTypes(r)[o];i=v(r.prototype.constructor).get(o),r=r.prototype,t.setType(a,r,i)}var s=null!==(n=t.getJsonPropertiesMetadata(r))&&void 0!==n?n:{};s[i]=h(i,e),t.setJsonPropertiesMetadata(s,r)}},exports.JsonSerializer=f,exports.JsonSerializerOptions=p,exports.isNullish=s,exports.logError=d,exports.throwError=function(e){throw new Error(e)};

@@ -1,1 +0,1 @@

import{__spreadArray as e,__assign as t}from"tslib";import"reflect-metadata";var r=function(){function e(){}return e.getBaseClass=function(e){return e?Reflect.getPrototypeOf(e):void 0},e.getJsonPropertiesMetadata=function(t,r){if(t){var i=""+e.apiMap+(r||t.constructor.name);return Reflect.getMetadata(i,t)}},e.getParamTypes=function(t){return t?Reflect.getMetadata(e.designParamTypes,t):void 0},e.getJsonObjectMetadata=function(t){return t?Reflect.getMetadata(e.apiMapJsonObject,t):void 0},e.getType=function(t,r){return t?Reflect.getMetadata(e.designType,t,r):void 0},e.isJsonObject=function(t){return!!t&&Reflect.hasOwnMetadata(e.apiMapJsonObject,t)},e.setJsonPropertiesMetadata=function(t,r){if(r){var i=""+e.apiMap+r.constructor.name;Reflect.defineMetadata(i,t,r)}},e.setJsonObject=function(t,r){r&&Reflect.defineMetadata(e.apiMapJsonObject,t,r)},e.setType=function(t,r,i){r&&t&&Reflect.defineMetadata(e.designType,t,r,i)},e.apiMap="api:map:",e.apiMapJsonObject=e.apiMap+"jsonObject",e.designType="design:type",e.designParamTypes="design:paramtypes",e}(),i=function(e){return"string"==typeof e},o=function(e){return null!==e&&"object"==typeof e&&!n(e)},n=function(e){return Array.isArray(e)},a=function(e){return"[object Date]"===toString.call(e)},s=function(e){return r.isJsonObject(e)},l=function(e){return[null,void 0].includes(e)},u=function(e){return e instanceof Set},c=function(e){try{var t=JSON.parse(e);return"object"==typeof t?t:e}catch(t){return e}},p=function(e,t){return e.filter((function(e){return!t.some((function(t){return e===t}))}))},d=function(){this.errorCallback=y,this.nullishPolicy={undefined:"remove",null:"allow"},this.additionalPropertiesPolicy="remove"},f=function(e){throw new Error(e)},y=function(e){console.error(e)},h=function(){function f(e){this.options=new d,this.options=t(t({},this.options),e)}return f.prototype.deserialize=function(e,t){return i(e)&&(e=c(e)),n(e)?this.deserializeObjectArray(e,t):o(e)?this.deserializeObject(e,t):void this.error("Fail to deserialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},f.prototype.deserializeObject=function(e,t){var r=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Object."),null;if(void 0!==e){if(i(e)&&(e=c(e)),o(e)){var n=function(e){if("function"!=typeof e)return!1;try{Reflect.construct(String,[],e)}catch(e){return!1}return!0}(t)?new t({}):t,a=this.getJsonPropertiesMetadata(n);if(!a)return n;var s=Object.keys(a);if(s.forEach((function(t){var i=a[t],o=r.deserializeProperty(n,t,e,i);r.checkRequiredProperty(i,n,t,o,e,!1),r.isAllowedProperty(t,o)&&(n[t]=o)})),"remove"===this.options.additionalPropertiesPolicy)return n;var l=p(Object.keys(e),s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return n[t]=e[t]})),n):n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Object'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Object.")},f.prototype.deserializeObjectArray=function(e,t){var r=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Array."),null;if(void 0!==e){if(i(e)&&(e=c(e)),n(e))return e.reduce((function(e,i){var o=r.deserializeObject(i,t);return(!l(o)||null===o&&"remove"!==r.options.nullishPolicy.null||void 0===o&&"remove"!==r.options.nullishPolicy.undefined)&&e.push(o),e}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Array.")},f.prototype.serialize=function(e){return n(e)?this.serializeObjectArray(e):o(e)?this.serializeObject(e):void this.error("Fail to serialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},f.prototype.serializeObject=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Object."),null;if(void 0!==e){if(!o(e))return e;var r=this.getJsonPropertiesMetadata(e);if(!r)return e;var i={},a=Object.keys(e),s=Object.keys(r);if(s.forEach((function(o){var s=r[o];if(a.includes(o)){var l=void 0;s.beforeSerialize&&(l=e[o],e[o]=s.beforeSerialize(e[o],e));var u=t.serializeProperty(e,o,s);if(s.afterSerialize&&(u=s.afterSerialize(u,e)),e[o]=l||e[o],n(s.name))s.name.forEach((function(e){t.isAllowedProperty(e,u[e])&&(i[e]=u[e])}));else if(t.checkRequiredProperty(s,e,o,u,e),t.isAllowedProperty(o,u))if(s.isNameOverridden||void 0===t.options.formatPropertyName)i[s.name]=u;else{var c=t.options.formatPropertyName(s.name);i[c]=u}}else n(s.name)?s.name.forEach((function(e){t.isAllowedProperty(e,void 0)&&(i[e]=void 0)})):(t.checkRequiredProperty(s,e,o,void 0,e),t.isAllowedProperty(o,void 0)&&(i[s.name]=void 0))})),"remove"===this.options.additionalPropertiesPolicy)return i;var l=p(a,s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return i[t]=e[t]})),i):i}"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Object.")},f.prototype.serializeObjectArray=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Array."),null;if(void 0!==e){if(n(e))return e.reduce((function(e,r){var i=t.serializeObject(r);return(!l(i)||null===i&&"remove"!==t.options.nullishPolicy.null||void 0===i&&"remove"!==t.options.nullishPolicy.undefined)&&e.push(i),e}),[]);this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e)+".")}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Array.")},f.prototype.checkRequiredProperty=function(e,t,r,i,o,n){if(void 0===n&&(n=!0),e.required&&l(i)){var a=t.constructor.name;this.error("Fail to "+(n?"serialize":"deserialize")+": Property '"+r+"' is required in "+a+" "+JSON.stringify(o)+".")}},f.prototype.deserializeProperty=function(e,t,i,o){if(!l(i)){var n=this.getDataSource(i,o,this.options.formatPropertyName);if(l(n))return n;var a,u=r.getType(e,t),c=this.getDataStructureInformation(u,e[t],o),p=c.isArrayProperty,d=c.isSetProperty,f=c.isMapProperty,y=c.isDictionaryProperty,h=o.type||u;o.beforeDeserialize&&(n=o.beforeDeserialize(n,e));var v=o.predicate;return y||f?(a=this.deserializeDictionary(n,h,v),f&&(a=new Map(Object.entries(a)))):p||d?(a=this.deserializeArray(n,h,v),d&&(a=new Set(a))):!s(h)&&!v||v&&!v(n,i)?a=this.deserializePrimitive(n,h.name):(h=o.predicate?o.predicate(n,i):h,a=this.deserializeObject(n,h)),o.afterDeserialize&&(a=o.afterDeserialize(a,e)),a}},f.prototype.deserializePrimitive=function(e,t){if(l(t))return e;if(typeof e===(t=t.toLowerCase()))return e;var r="Fail to deserialize: type '"+typeof e+"' is not assignable to type '"+t+"'.\nReceived: "+JSON.stringify(e);switch(t){case"string":var i=e.toString();return"[object Object]"===i?void this.error(r):i;case"number":return function(e){return"number"==typeof e}(e)?+e:void this.error(r);case"boolean":return void this.error(r);case"date":return function(e){return!a(e)&&!n(e)&&!isNaN(Date.parse(e))}(e)?new Date(e):void this.error(r);default:return e}},f.prototype.deserializeDictionary=function(e,t,r){var i=this;if(o(e)){var n={};return Object.keys(e).forEach((function(o){var a=r?r(e[o],e):void 0;s(t)||a?n[o]=i.deserializeObject(e[o],a||t):n[o]=i.deserializePrimitive(e[o],typeof e[o])})),n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},f.prototype.deserializeArray=function(e,t,r){var i=this;if(n(e))return e.reduce((function(o,n){var a;return s(t)||r?(t=r?r(n,e):t,a=i.deserializeObject(n,t)):a=i.deserializePrimitive(n,typeof n),(!l(a)||null===a&&"remove"!==i.options.nullishPolicy.null||void 0===a&&"remove"!==i.options.nullishPolicy.undefined)&&o.push(a),o}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))},f.prototype.error=function(e){this.options.errorCallback&&this.options.errorCallback(e)},f.prototype.getClassesJsonPropertiesMetadata=function(e,t){return e?e.reduce((function(e,i){var o=r.getJsonPropertiesMetadata(t,i);return o&&e.push(o),e}),[]):[]},f.prototype.getDataSource=function(e,t,r){var i=t.name,o=t.isNameOverridden;if(n(i)){var a={};return i.forEach((function(t){return a[t]=e[t]})),a}return!o&&r?(i=r(i),e[i]):e[i]},f.prototype.getDataStructureInformation=function(e,t,r){var i,o,a,s,l;if(r.dataStructure)return{isArrayProperty:null!==(i="array"===r.dataStructure)&&void 0!==i&&i,isDictionaryProperty:null!==(o="dictionary"===r.dataStructure)&&void 0!==o&&o,isMapProperty:null!==(a="map"===r.dataStructure)&&void 0!==a&&a,isSetProperty:null!==(s="set"===r.dataStructure)&&void 0!==s&&s};var c,p=null===(l=null==e?void 0:e.name)||void 0===l?void 0:l.toLowerCase();return"object"===p?{isArrayProperty:n(t),isDictionaryProperty:!1,isMapProperty:(c=t,c instanceof Map),isSetProperty:u(t)}:{isArrayProperty:"array"===p,isDictionaryProperty:!1,isMapProperty:"map"===p,isSetProperty:"set"===p}},f.prototype.getJsonPropertiesMetadata=function(t){var i,o=(null!==(i=r.getJsonObjectMetadata(t.constructor))&&void 0!==i?i:{}).baseClassNames,n=r.getJsonPropertiesMetadata(t);if(!(n||o&&o.length))return n;if(o&&o.length){var a=this.getClassesJsonPropertiesMetadata(o,t);return this.mergeJsonPropertiesMetadata.apply(this,e(e([],a),[n]))}return n},f.prototype.isAllowedProperty=function(e,t){if(l(t)){if("disallow"===this.options.nullishPolicy[""+t])return this.error("Disallowed "+t+" value detected: "+e+"."),!1;if("remove"===this.options.nullishPolicy[""+t])return!1}return!0},f.prototype.mergeJsonPropertiesMetadata=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var i={};return e.forEach((function(e){e&&Object.keys(e).forEach((function(r){i[r]=t(t({},i[r]),e[r])}))})),i},f.prototype.serializeDictionary=function(e){var t=this;if(o(e)){var r={};return Object.keys(e).forEach((function(i){r[i]=t.serializeObject(e[i])})),r}this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},f.prototype.serializeProperty=function(e,t,o){var n,l=this,u=e[t],c=r.getType(e,t),p=this.getDataStructureInformation(c,u,o),d=p.isArrayProperty,f=p.isDictionaryProperty,y=p.isMapProperty,h=p.isSetProperty,v=o.predicate,g=o.type||c,P=s(g);if(u&&(P||v)){if(d||h){var b=h?Array.from(u):u;return this.serializeObjectArray(b)}if(f||y){if(!y)return this.serializeDictionary(u);var m={};return u.forEach((function(e,t){i(t)?m[t]=e:l.error("Fail to serialize: type of '"+typeof t+"' is not assignable to type 'string'.\nReceived: "+JSON.stringify(t)+".")})),this.serializeDictionary(m)}return this.serializeObject(u)}return"date"===(null===(n=null==g?void 0:g.name)||void 0===n?void 0:n.toLocaleLowerCase())&&a(u)?u.toISOString():u},f}(),v=function(t){var i=r.getBaseClass(t);return i&&i.name?e(e([],v(i)),[i.name]):[]},g=function(){return function(e){var t=v(e);r.setJsonObject({baseClassNames:t},e)}},P=function(e){return function(t,i,o){var n;if(void 0===i&&t.prototype){var a=r.getParamTypes(t)[o];i=b(t.prototype.constructor).get(o),t=t.prototype,r.setType(a,t,i)}var s=null!==(n=r.getJsonPropertiesMetadata(t))&&void 0!==n?n:{};s[i]=m(i,e),r.setJsonPropertiesMetadata(s,t)}},b=function(e){var t,r=e.toString().split("}")[0].replace(/(\/\*[\s\S]*?\*\/|\/\/.*$)/gm,"").replace(/[\r\t\n\v\f ]/g,""),i=r.length;","===r[i-2]&&(t=r[i-1]);var o=t?new RegExp("(?:(this|"+t+"|\\("+t+"=t.call\\(this(,.)*\\)\\))\\.)([^,;\n}]+)","gm"):new RegExp("(?:(this)\\.)([^,;\n}]+)","gm"),n=new Map,a=/(?:.*(?:constructor|function).*?(?=\())(?:\()(.+?(?=\)))/m.exec(r);if(!a||!a.length)return n;for(var s,l=a[1].split(","),u=function(){var e=s.length-1,t=s[e].split("="),r=l.findIndex((function(e){return e===t[1]}));r>-1&&n.set(r,t[0])};s=o.exec(r);)u();return n},m=function(e,n){var a={name:e.toString()};return n?i(n)?(a.name=n,a.isNameOverridden=!0,a):(o(n)&&(a=t(t({},a),n),n.name&&(a.name=n.name,a.isNameOverridden=!0),function(e){if(!e)return!1;var t=r.getParamTypes(e),i=e.length;return(1===i||2===i)&&!t}(n.type)&&(delete a.type,a.predicate=n.type)),a):a};export{g as JsonObject,P as JsonProperty,h as JsonSerializer,d as JsonSerializerOptions,l as isNullish,y as logError,f as throwError};
import{__spreadArray as e,__assign as t}from"tslib";import"reflect-metadata";var r=function(){function e(){}return e.getBaseClass=function(e){return e?Reflect.getPrototypeOf(e):void 0},e.getJsonPropertiesMetadata=function(t,r){if(t){var i=""+e.apiMap+(r||t.constructor.name);return Reflect.getMetadata(i,t)}},e.getParamTypes=function(t){return t?Reflect.getMetadata(e.designParamTypes,t):void 0},e.getJsonObjectMetadata=function(t){return t?Reflect.getMetadata(e.apiMapJsonObject,t):void 0},e.getType=function(t,r){return t?Reflect.getMetadata(e.designType,t,r):void 0},e.isJsonObject=function(t){return!!t&&Reflect.hasOwnMetadata(e.apiMapJsonObject,t)},e.setJsonPropertiesMetadata=function(t,r){if(r){var i=""+e.apiMap+r.constructor.name;Reflect.defineMetadata(i,t,r)}},e.setJsonObject=function(t,r){r&&Reflect.defineMetadata(e.apiMapJsonObject,t,r)},e.setType=function(t,r,i){r&&t&&Reflect.defineMetadata(e.designType,t,r,i)},e.apiMap="api:map:",e.apiMapJsonObject=e.apiMap+"jsonObject",e.designType="design:type",e.designParamTypes="design:paramtypes",e}(),i=function(e){return"string"==typeof e},o=function(e){return null!==e&&"object"==typeof e&&!n(e)},n=function(e){return Array.isArray(e)},a=function(e){return"[object Date]"===toString.call(e)},s=function(e){return r.isJsonObject(e)},l=function(e){return[null,void 0].includes(e)},u=function(e){return e instanceof Set},c=function(e){try{var t=JSON.parse(e);return"object"==typeof t?t:e}catch(t){return e}},p=function(e,t){return e.filter((function(e){return!t.some((function(t){return e===t}))}))},d=function(){this.errorCallback=y,this.nullishPolicy={undefined:"remove",null:"allow"},this.additionalPropertiesPolicy="remove"},f=function(e){throw new Error(e)},y=function(e){console.error(e)},v=function(){function f(e){this.options=new d,this.options=t(t({},this.options),e)}return f.prototype.deserialize=function(e,t){return i(e)&&(e=c(e)),n(e)?this.deserializeObjectArray(e,t):o(e)?this.deserializeObject(e,t):void this.error("Fail to deserialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},f.prototype.deserializeObject=function(t,n){var a,s=this;if(null===t)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Object."),null;if(void 0!==t){if(i(t)&&(t=c(t)),o(t)){var u;if(function(e){if("function"!=typeof e)return!1;try{Reflect.construct(String,[],e)}catch(e){return!1}return!0}(n)){var d=Object.create(n.prototype),f=r.getJsonObjectMetadata(d.constructor),y=null!==(a=null==f?void 0:f.constructorParams)&&void 0!==a?a:[];u=new(n.bind.apply(n,e([void 0],y)))}else u=n;var v=this.getJsonPropertiesMetadata(u);if(!v)return u;var h=Object.keys(v);if(h.forEach((function(e){var r=v[e],i=s.deserializeProperty(u,e,t,r);s.checkRequiredProperty(r,u,e,i,t,!1);var o=u[e];i===o||(null!==i||void 0!==o)&&l(i)||(o=i),s.isAllowedProperty(e,o)&&(u[e]=o)})),"remove"===this.options.additionalPropertiesPolicy)return u;var P=p(Object.keys(t),h);return P.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(t)+": "+P+"."):"allow"===this.options.additionalPropertiesPolicy&&P.forEach((function(e){return u[e]=t[e]})),u):u}this.error("Fail to deserialize: type '"+typeof t+"' is not assignable to type 'Object'.\nReceived: "+JSON.stringify(t))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Object.")},f.prototype.deserializeObjectArray=function(e,t){var r=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to deserialize: null is not assignable to type Array."),null;if(void 0!==e){if(i(e)&&(e=c(e)),n(e))return e.reduce((function(e,i){var o=r.deserializeObject(i,t);return(!l(o)||null===o&&"remove"!==r.options.nullishPolicy.null||void 0===o&&"remove"!==r.options.nullishPolicy.undefined)&&e.push(o),e}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to deserialize: undefined is not assignable to type Array.")},f.prototype.serialize=function(e){return n(e)?this.serializeObjectArray(e):o(e)?this.serializeObject(e):void this.error("Fail to serialize: value is not an Array nor an Object.\nReceived: "+JSON.stringify(e)+".")},f.prototype.serializeObject=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Object."),null;if(void 0!==e){if(!o(e))return e;var r=this.getJsonPropertiesMetadata(e);if(!r)return e;var i={},a=Object.keys(e),s=Object.keys(r);if(s.forEach((function(o){var s=r[o];if(a.includes(o)){var l=void 0;s.beforeSerialize&&(l=e[o],e[o]=s.beforeSerialize(e[o],e));var u=t.serializeProperty(e,o,s);if(s.afterSerialize&&(u=s.afterSerialize(u,e)),e[o]=l||e[o],n(s.name))s.name.forEach((function(e){t.isAllowedProperty(e,u[e])&&(i[e]=u[e])}));else if(t.checkRequiredProperty(s,e,o,u,e),t.isAllowedProperty(o,u))if(s.isNameOverridden||void 0===t.options.formatPropertyName)i[s.name]=u;else{var c=t.options.formatPropertyName(s.name);i[c]=u}}else n(s.name)?s.name.forEach((function(e){t.isAllowedProperty(e,void 0)&&(i[e]=void 0)})):(t.checkRequiredProperty(s,e,o,void 0,e),t.isAllowedProperty(o,void 0)&&(i[s.name]=void 0))})),"remove"===this.options.additionalPropertiesPolicy)return i;var l=p(a,s);return l.length?("disallow"===this.options.additionalPropertiesPolicy?this.error("Additional properties detected in "+JSON.stringify(e)+": "+l+"."):"allow"===this.options.additionalPropertiesPolicy&&l.forEach((function(t){return i[t]=e[t]})),i):i}"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Object.")},f.prototype.serializeObjectArray=function(e){var t=this;if(null===e)return"disallow"===this.options.nullishPolicy.null&&this.error("Fail to serialize: null is not assignable to type Array."),null;if(void 0!==e){if(n(e))return e.reduce((function(e,r){var i=t.serializeObject(r);return(!l(i)||null===i&&"remove"!==t.options.nullishPolicy.null||void 0===i&&"remove"!==t.options.nullishPolicy.undefined)&&e.push(i),e}),[]);this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e)+".")}else"disallow"===this.options.nullishPolicy.undefined&&this.error("Fail to serialize: undefined is not assignable to type Array.")},f.prototype.checkRequiredProperty=function(e,t,r,i,o,n){if(void 0===n&&(n=!0),e.required&&l(i)&&l(t[r])){var a=t.constructor.name;this.error("Fail to "+(n?"serialize":"deserialize")+": Property '"+r+"' is required in "+a+" "+JSON.stringify(o)+".")}},f.prototype.deserializeProperty=function(e,t,i,o){if(!l(i)){var n=this.getDataSource(i,o,this.options.formatPropertyName);if(l(n))return n;var a,u=r.getType(e,t),c=this.getDataStructureInformation(u,e[t],o),p=c.isArrayProperty,d=c.isSetProperty,f=c.isMapProperty,y=c.isDictionaryProperty,v=o.type||u;o.beforeDeserialize&&(n=o.beforeDeserialize(n,e));var h=o.predicate;return y||f?(a=this.deserializeDictionary(n,v,h),f&&(a=new Map(Object.entries(a)))):p||d?(a=this.deserializeArray(n,v,h),d&&(a=new Set(a))):!s(v)&&!h||h&&!h(n,i)?a=this.deserializePrimitive(n,v.name):(v=o.predicate?o.predicate(n,i):v,a=this.deserializeObject(n,v)),o.afterDeserialize&&(a=o.afterDeserialize(a,e)),a}},f.prototype.deserializePrimitive=function(e,t){if(l(t))return e;if(typeof e===(t=t.toLowerCase()))return e;var r="Fail to deserialize: type '"+typeof e+"' is not assignable to type '"+t+"'.\nReceived: "+JSON.stringify(e);switch(t){case"string":var i=e.toString();return"[object Object]"===i?void this.error(r):i;case"number":return function(e){return"number"==typeof e}(e)?+e:void this.error(r);case"boolean":return void this.error(r);case"date":return function(e){return!a(e)&&!n(e)&&!isNaN(Date.parse(e))}(e)?new Date(e):void this.error(r);default:return e}},f.prototype.deserializeDictionary=function(e,t,r){var i=this;if(o(e)){var n={};return Object.keys(e).forEach((function(o){var a=r?r(e[o],e):void 0;s(t)||a?n[o]=i.deserializeObject(e[o],a||t):n[o]=i.deserializePrimitive(e[o],typeof e[o])})),n}this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},f.prototype.deserializeArray=function(e,t,r){var i=this;if(n(e))return e.reduce((function(o,n){var a;return s(t)||r?(t=r?r(n,e):t,a=i.deserializeObject(n,t)):a=i.deserializePrimitive(n,typeof n),(!l(a)||null===a&&"remove"!==i.options.nullishPolicy.null||void 0===a&&"remove"!==i.options.nullishPolicy.undefined)&&o.push(a),o}),[]);this.error("Fail to deserialize: type '"+typeof e+"' is not assignable to type 'Array'.\nReceived: "+JSON.stringify(e))},f.prototype.error=function(e){this.options.errorCallback&&this.options.errorCallback(e)},f.prototype.getClassesJsonPropertiesMetadata=function(e,t){return e?e.reduce((function(e,i){var o=r.getJsonPropertiesMetadata(t,i);return o&&e.push(o),e}),[]):[]},f.prototype.getDataSource=function(e,t,r){var i=t.name,o=t.isNameOverridden;if(n(i)){var a={};return i.forEach((function(t){return a[t]=e[t]})),a}return!o&&r?(i=r(i),e[i]):e[i]},f.prototype.getDataStructureInformation=function(e,t,r){var i,o,a,s,l;if(r.dataStructure)return{isArrayProperty:null!==(i="array"===r.dataStructure)&&void 0!==i&&i,isDictionaryProperty:null!==(o="dictionary"===r.dataStructure)&&void 0!==o&&o,isMapProperty:null!==(a="map"===r.dataStructure)&&void 0!==a&&a,isSetProperty:null!==(s="set"===r.dataStructure)&&void 0!==s&&s};var c,p=null===(l=null==e?void 0:e.name)||void 0===l?void 0:l.toLowerCase();return"object"===p?{isArrayProperty:n(t),isDictionaryProperty:!1,isMapProperty:(c=t,c instanceof Map),isSetProperty:u(t)}:{isArrayProperty:"array"===p,isDictionaryProperty:!1,isMapProperty:"map"===p,isSetProperty:"set"===p}},f.prototype.getJsonPropertiesMetadata=function(t){var i,o=(null!==(i=r.getJsonObjectMetadata(t.constructor))&&void 0!==i?i:{}).baseClassNames,n=r.getJsonPropertiesMetadata(t);if(!(n||o&&o.length))return n;if(o&&o.length){var a=this.getClassesJsonPropertiesMetadata(o,t);return this.mergeJsonPropertiesMetadata.apply(this,e(e([],a),[n]))}return n},f.prototype.isAllowedProperty=function(e,t){if(l(t)){if("disallow"===this.options.nullishPolicy[""+t])return this.error("Disallowed "+t+" value detected: "+e+"."),!1;if("remove"===this.options.nullishPolicy[""+t])return!1}return!0},f.prototype.mergeJsonPropertiesMetadata=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var i={};return e.forEach((function(e){e&&Object.keys(e).forEach((function(r){i[r]=t(t({},i[r]),e[r])}))})),i},f.prototype.serializeDictionary=function(e){var t=this;if(o(e)){var r={};return Object.keys(e).forEach((function(i){r[i]=t.serializeObject(e[i])})),r}this.error("Fail to serialize: type '"+typeof e+"' is not assignable to type 'Dictionary'.\nReceived: "+JSON.stringify(e)+".")},f.prototype.serializeProperty=function(e,t,o){var n,l=this,u=e[t],c=r.getType(e,t),p=this.getDataStructureInformation(c,u,o),d=p.isArrayProperty,f=p.isDictionaryProperty,y=p.isMapProperty,v=p.isSetProperty,h=o.predicate,P=o.type||c,g=s(P);if(u&&(g||h)){if(d||v){var b=v?Array.from(u):u;return this.serializeObjectArray(b)}if(f||y){if(!y)return this.serializeDictionary(u);var m={};return u.forEach((function(e,t){i(t)?m[t]=e:l.error("Fail to serialize: type of '"+typeof t+"' is not assignable to type 'string'.\nReceived: "+JSON.stringify(t)+".")})),this.serializeDictionary(m)}return this.serializeObject(u)}return"date"===(null===(n=null==P?void 0:P.name)||void 0===n?void 0:n.toLocaleLowerCase())&&a(u)?u.toISOString():u},f}(),h=function(t){var i=r.getBaseClass(t);return i&&i.name?e(e([],h(i)),[i.name]):[]},P=function(e){return function(t){var i,o=h(t),n=null!==(i=null==e?void 0:e.constructorParams)&&void 0!==i?i:[];r.setJsonObject({baseClassNames:o,constructorParams:n},t)}},g=function(e){return function(t,i,o){var n;if(void 0===i&&t.prototype){var a=r.getParamTypes(t)[o];i=b(t.prototype.constructor).get(o),t=t.prototype,r.setType(a,t,i)}var s=null!==(n=r.getJsonPropertiesMetadata(t))&&void 0!==n?n:{};s[i]=m(i,e),r.setJsonPropertiesMetadata(s,t)}},b=function(e){var t,r=e.toString().split("}")[0].replace(/(\/\*[\s\S]*?\*\/|\/\/.*$)/gm,"").replace(/[\r\t\n\v\f ]/g,""),i=r.length;","===r[i-2]&&(t=r[i-1]);var o=t?new RegExp("(?:(this|"+t+"|\\("+t+"=t.call\\(this(,.)*\\)\\))\\.)([^,;\n}]+)","gm"):new RegExp("(?:(this)\\.)([^,;\n}]+)","gm"),n=new Map,a=/(?:.*(?:constructor|function).*?(?=\())(?:\()(.+?(?=\)))/m.exec(r);if(!a||!a.length)return n;for(var s,l=a[1].split(","),u=function(){var e=s.length-1,t=s[e].split("="),r=l.findIndex((function(e){return e===t[1]}));r>-1&&n.set(r,t[0])};s=o.exec(r);)u();return n},m=function(e,n){var a={name:e.toString()};return n?i(n)?(a.name=n,a.isNameOverridden=!0,a):(o(n)&&(a=t(t({},a),n),n.name&&(a.name=n.name,a.isNameOverridden=!0),function(e){if(!e)return!1;var t=r.getParamTypes(e),i=e.length;return(1===i||2===i)&&!t}(n.type)&&(delete a.type,a.predicate=n.type)),a):a};export{P as JsonObject,g as JsonProperty,v as JsonSerializer,d as JsonSerializerOptions,l as isNullish,y as logError,f as throwError};
export interface JsonObjectMetadata {
baseClassNames: Array<string>;
constructorParams: Array<unknown>;
}
export declare const JsonObject: () => Function;
export interface JsonObjectOptions {
constructorParams?: Array<unknown>;
}
export declare const JsonObject: (options?: JsonObjectOptions | undefined) => Function;

@@ -15,3 +15,3 @@ export declare type IOProto = (property: any, currentInstance?: any) => any;

}
export declare type JsonPropertyMetadata = {
export interface JsonPropertyMetadata extends JsonPropertyBaseMetadata {
isNameOverridden?: boolean;

@@ -21,6 +21,7 @@ name: string | Array<string>;

predicate?: PredicateProto;
} & JsonPropertyBaseMetadata;
export declare const JsonProperty: (options?: string | ({
name?: string | string[] | undefined;
type?: Function | PredicateProto | undefined;
} & JsonPropertyBaseMetadata) | undefined) => Function;
}
export interface JsonPropertyOptions extends JsonPropertyBaseMetadata {
name?: string | Array<string>;
type?: Function | PredicateProto;
}
export declare const JsonProperty: (options?: string | JsonPropertyOptions | undefined) => Function;
{
"name": "typescript-json-serializer",
"version": "5.1.0",
"version": "6.0.0",
"description": "Typescript library to serialize classes into json and deserialize json into classes.",

@@ -5,0 +5,0 @@ "source": "src/index.ts",

@@ -5,3 +5,3 @@ # typescript-json-serializer

![npm](https://img.shields.io/npm/dt/typescript-json-serializer)
![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/typescript-json-serializer/5.1.0)
![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/typescript-json-serializer/6.0.0)
[![Coverage Status](https://coveralls.io/repos/github/GillianPerard/typescript-json-serializer/badge.svg)](https://coveralls.io/github/GillianPerard/typescript-json-serializer)

@@ -63,9 +63,9 @@ [![Known Vulnerabilities](https://snyk.io/test/github/gillianperard/typescript-json-serializer/badge.svg?targetFile=package.json)](https://snyk.io/test/github/gillianperard/typescript-json-serializer?targetFile=package.json)

null: 'allow'
};
},
// Disallow additional properties (non JsonProperty)
additionalPropertiesPolicy: 'disallow'
additionalPropertiesPolicy: 'disallow',
// e.g. if all the properties in the json object are prefixed by '_'
formatPropertyName: (propertyName: string) => `_${propertyName}`;
formatPropertyName: (propertyName: string) => `_${propertyName}`,
})

@@ -174,9 +174,7 @@

// Create a JsonObject class: Animal
// Create a JsonObject class that extends LivingBeing: Animal
@JsonObject()
export class Animal {
@JsonProperty() id: number;
@JsonProperty() name: string;
export class Animal extends LivingBeing {
@JsonProperty() name: string | undefined;
@JsonProperty() birthDate: Date;

@@ -193,3 +191,4 @@ @JsonProperty() numberOfPaws: number;

constructor(name: string) {
constructor(name: string | undefined) {
super();
this.name = name;

@@ -211,3 +210,3 @@ }

public constructor(
name: string,
name: string | undefined,
@JsonProperty() public isSpeckled: boolean

@@ -221,6 +220,10 @@ ) {

// Create a JsonObject class that extends Animal
// (which extends LivingBeing): Snake
// Create a JsonObject class that extends Animal (which extends LivingBeing): Snake
@JsonObject()
// When the library instatiates a class during deserilization it does not know the constructor params value
// so you need to specify them.
// To ways are available
// - by passing default value to those params in the constructor
// - by passing the `constructorParams` options to `JsonObject` (see below); it allows to keep clean constructor
@JsonObject({ constructorParams: [{}] })
export class Snake extends Animal {

@@ -230,3 +233,3 @@

public constructor(args: { name: string; isPoisonous: boolean }) {
public constructor(args: { name: string | undefined; isPoisonous: boolean }) {
super(args.name);

@@ -239,4 +242,3 @@ this.isPoisonous = args.isPoisonous;

// Create a JsonObject empty class that extends Animal
// (which extends LivingBeing): UnknownAnimal
// Create a JsonObject empty class that extends Animal (which extends LivingBeing): UnknownAnimal

@@ -408,3 +410,5 @@ @JsonObject()

export class Society {
@JsonProperty() id: string;
// Here note the `required` option with the default value `'4'`
// it will not throw because the default value will be set instead of `undefined`
@JsonProperty({ required: true }) id: string = '4';
@JsonProperty() name: string;

@@ -419,3 +423,2 @@ }

export const data: any = {
id: '1',
name: 'Zoos Organization',

@@ -488,3 +491,2 @@ zoosName: {

id: 2,
name: 'Jolene',
birthDate: '2017-03-10T22:00:00.000Z',

@@ -587,6 +589,13 @@ numberOfPaws: 4,

##### **Parameters**
**options**
Type: [`JsonObjectOptions`](#jsonobjectoptions)
Optional: `true`
Description: The option to customize the serialization/deserialization of the target class.
##### **Example**
```typescript
@JsonObject()
@JsonObject(options)
class MyClass {}

@@ -794,2 +803,8 @@ ```

##### **JsonObjectOptions**
```typescript
constructorParams?: Array<unknown>;
```
##### **JsonPropertyOptions**

@@ -847,3 +862,3 @@

#### **Functions types**
#### **Function types**

@@ -850,0 +865,0 @@ ##### **ErrorCallback**

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc