json2typescript
Advanced tools
Comparing version 1.2.5 to 1.3.0
@@ -0,1 +1,15 @@ | ||
# v1.3.0 (2020-04-13) | ||
## Bug Fixes | ||
* Fixed content of `package-lock.json`, closes #125 | ||
## Features | ||
* Implemented `ignoreRequiredCheck` flag and allow serialization without having instances, closes [#126](https://github.com/AppVision-GmbH/json2typescript/pull/126) | ||
## Breaking Changes | ||
* In previous version, the serialization of plain objects failed, because instances of classes were required. As of now, the serialization of plain object may succeed if the structure matches the class structure. Thus, this is considered a soft breaking change. | ||
# v1.2.5 (2020-03-01) | ||
@@ -2,0 +16,0 @@ |
@@ -1,2 +0,2 @@ | ||
Copyright (c) 2018 Andreas Aeschlimann | ||
Copyright (c) 2020 Andreas Aeschlimann | ||
@@ -19,2 +19,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
SOFTWARE. |
{ | ||
"name": "json2typescript", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "Provides TypeScript methods to map a JSON object to a JavaScript object on runtime", | ||
@@ -33,11 +33,11 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/jasmine": "^3.3.7", | ||
"jasmine": "3.3.1", | ||
"karma": "^4.4.1", | ||
"karma-jasmine": "2.0.1", | ||
"karma-mocha-reporter": "^2.2.3", | ||
"karma-phantomjs-launcher": "1.0.4", | ||
"karma-typescript": "^4.0.0", | ||
"@types/jasmine": "^3.5.10", | ||
"jasmine": "3.5.0", | ||
"karma": "^5.0.1", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-jasmine": "3.1.1", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-typescript": "^5.0.2", | ||
"phantomjs-prebuilt": "^2.1.16", | ||
"typescript": "3.3.3" | ||
"typescript": "3.8.3" | ||
}, | ||
@@ -44,0 +44,0 @@ "main": "index.js", |
@@ -362,4 +362,5 @@ [![npm](https://img.shields.io/npm/v/json2typescript.svg)](https://www.npmjs.com/package/json2typescript) | ||
> Tip: Some API's return null instead of omitting optional values. | ||
If you flag a property as optional, `json2typescript` will ignore null values and keep the default value of the property instead. | ||
If you flag a property as optional, `json2typescript` will ignore (JSON) null values at deserialization and keep the default value of the property instead. | ||
This fact is particularly helpful if your project uses TypeScript `strictNullChecks` or/and disallows `null` values through the `valueCheckingMode` property in `json2typescript`. | ||
Furthermore, note that the instance property `ignoreRequiredCheck` – if set to true – overrides all `isOptional` values for all properties. | ||
@@ -486,2 +487,12 @@ #### Important notes | ||
#### Ignore required property checks | ||
`(bool) JsonConvertignoreRequiredCheck` | ||
Determines whether the check for required properties should be ignored, making all mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
If true, any missing properties (undefined) when serializing or deserializing will be ignored, as if they were marked optional. | ||
Note that properties explicitly set to null will be unaffected by this flag – they will be ignored if optional and included if not. | ||
The default is `false`. | ||
### Public methods | ||
@@ -493,3 +504,3 @@ | ||
`(any) serialize(data: T | T[])` | ||
`(any) serialize(data: any | any[])` | ||
@@ -512,4 +523,4 @@ Tries to serialize a TypeScript object or array of objects to JSON. | ||
In case you would like to force `json2typescript` to use a specific way, you can use the following methods instead: | ||
- `(any) serializeObject(instance: T)` | ||
- `(any[]) serializeArray(instanceArray: T[])` | ||
- `(any) serializeObject(instance: any)` | ||
- `(any[]) serializeArray(instanceArray: any[])` | ||
- `(T) deserializeObject(jsonObject: any, classReference: { new(): T })` | ||
@@ -516,0 +527,0 @@ - `(T[]) deserializeArray(jsonArray: any[], classReference: { new(): T })` |
@@ -9,3 +9,2 @@ /** | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -25,3 +24,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -41,3 +39,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -44,0 +41,0 @@ */ |
@@ -11,3 +11,2 @@ "use strict"; | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -29,3 +28,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -47,3 +45,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -50,0 +47,0 @@ */ |
/** | ||
* Offers a simple API for mapping JSON objects to TypeScript/JavaScript classes and vice versa. | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM | ||
@@ -24,14 +23,17 @@ */ | ||
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged | ||
* @returns {number} | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get operationMode(): number; | ||
/** | ||
* Determines how the JsonConvert class instance should operate. | ||
* | ||
* You may assign three different values: | ||
* - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done | ||
* - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged | ||
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged | ||
* @param value | ||
*/ | ||
operationMode: number; | ||
* Determines how the JsonConvert class instance should operate. | ||
* | ||
* You may assign three different values: | ||
* - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done | ||
* - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged | ||
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set operationMode(value: number); | ||
/** | ||
@@ -54,15 +56,16 @@ * Determines which types are allowed to be null. | ||
* | ||
* @returns {number} | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get valueCheckingMode(): number; | ||
/** | ||
* Determines which types are allowed to be null. | ||
* | ||
* You may assign three different values: | ||
* - ValueCheckingMode.ALLOW_NULL: all given values in the JSON are allowed to be null | ||
* - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null | ||
* - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON | ||
* | ||
* @param value | ||
*/ | ||
valueCheckingMode: number; | ||
* Determines which types are allowed to be null. | ||
* | ||
* You may assign three different values: | ||
* - ValueCheckingMode.ALLOW_NULL: all given values in the JSON are allowed to be null | ||
* - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null | ||
* - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set valueCheckingMode(value: number); | ||
/** | ||
@@ -77,11 +80,12 @@ * Determines whether primitive types should be checked. | ||
* | ||
* @returns {boolean} | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get ignorePrimitiveChecks(): boolean; | ||
/** | ||
* Determines whether primitive types should be checked. | ||
* If true, it will be allowed to assign primitive to other primitive types. | ||
* | ||
* @param value | ||
*/ | ||
ignorePrimitiveChecks: boolean; | ||
* Determines whether primitive types should be checked. | ||
* If true, it will be allowed to assign primitive to other primitive types. | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set ignorePrimitiveChecks(value: boolean); | ||
/** | ||
@@ -101,14 +105,42 @@ * Determines the rule of how JSON properties shall be matched with class properties during deserialization. | ||
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive | ||
* @returns {number} | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get propertyMatchingRule(): number; | ||
/** | ||
* Determines the rule of how JSON properties shall be matched with class properties during deserialization. | ||
* | ||
* You may assign the following values: | ||
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators | ||
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive | ||
* @param value | ||
*/ | ||
propertyMatchingRule: number; | ||
* Determines the rule of how JSON properties shall be matched with class properties during deserialization. | ||
* | ||
* You may assign the following values: | ||
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators | ||
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set propertyMatchingRule(value: number); | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties when serializing or deserializing will be ignored, as if they | ||
* were marked optional. | ||
*/ | ||
private _ignoreRequiredCheck; | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties (undefined) when serializing or deserializing will be | ||
* ignored, as if they were marked optional. | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get ignoreRequiredCheck(): boolean; | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties (undefined) when serializing or deserializing will be | ||
* ignored, as if they were marked optional. | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set ignoreRequiredCheck(value: boolean); | ||
/** | ||
* Constructor. | ||
@@ -125,5 +157,11 @@ * | ||
/** | ||
* Tries to serialize a TypeScript object or array of objects to JSON. | ||
* Tries to serialize a TypeScript object or array of objects to JSON using the mappings defined on | ||
* the specified class reference. Note that if a class reference is provided, it will be used as | ||
* the source of property mapping for serialization, even if the object or one of its elements is | ||
* an instance of a different class with its own mappings. Also, ONLY the properties from the | ||
* class reference will be serialized - any additional properties on the object(s) will be silently | ||
* ignored. | ||
* | ||
* @param data object or array of objects | ||
* @param classReference the class reference which provides the property mappings to use | ||
* | ||
@@ -134,10 +172,18 @@ * @returns the JSON object | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
serialize<T>(data: T | T[]): any | any[]; | ||
serialize<T>(data: any | any[], classReference?: { | ||
new (): T; | ||
}): any | any[]; | ||
/** | ||
* Tries to serialize a TypeScript object to a JSON object. | ||
* Tries to serialize a TypeScript object to a JSON object using either the mappings on the | ||
* provided class reference, if present, or on the provided object. Note that if a class | ||
* reference is provided, it will be used as the source of property mapping for serialization, | ||
* even if the object is itself an instance of a different class with its own mappings. | ||
* Also, ONLY the properties from the class reference will be serialized - any additional | ||
* properties on the object will be silently ignored. | ||
* | ||
* @param instance TypeScript instance | ||
* @param data object containing the values to be mapped to a JSON object, must be an | ||
* instance of a class with JSON mappings if no class reference is provided | ||
* @param classReference optional class reference which provides the property mappings to use | ||
* | ||
@@ -148,10 +194,18 @@ * @returns the JSON object | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
serializeObject<T>(instance: T): any; | ||
serializeObject<T>(data: any, classReference?: { | ||
new (): T; | ||
}): any; | ||
/** | ||
* Tries to serialize a TypeScript array to a JSON array. | ||
* Tries to serialize a TypeScript array to a JSON array using either the mappings on the | ||
* provided class reference, if present, or on the provided object. Note that if a class | ||
* reference is provided, ALL objects in the array will be serialized using the mappings | ||
* from that class reference, even if they're actually instances of a different class. | ||
* Also, ONLY the properties from the class reference will be serialized - any additional | ||
* properties on the objects will be silently ignored. | ||
* | ||
* @param instanceArray array of TypeScript instances | ||
* @param dataArray array of objects containing the values to be mapped to a JSON object, which | ||
* must be instances of classes with JSON mappings if no class reference is provided | ||
* @param classReference optional class reference which provides the property mappings to use | ||
* | ||
@@ -162,6 +216,7 @@ * @returns the JSON array | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
serializeArray<T>(instanceArray: T[]): any[]; | ||
serializeArray<T>(dataArray: any[], classReference?: { | ||
new (): T; | ||
}): any[]; | ||
/** | ||
@@ -177,3 +232,2 @@ * Tries to deserialize given JSON to a TypeScript object or array of objects. | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -194,3 +248,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -211,3 +264,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -219,8 +271,9 @@ */ | ||
/** | ||
* Tries to find the JSON mapping for a given class property and finally assign the value. | ||
* Tries to find the JSON mapping for a given class property from the given instance used for mapping, | ||
* and finally assign the value from the given dataObject | ||
* | ||
* @param instance the instance of the class | ||
* @param dataObject the object containing the value to be assigned | ||
* @param instance the instance of the class used for mapping | ||
* @param classPropertyName the property name | ||
* @param json the JSON object | ||
* | ||
* @throws throws an Error in case of failure | ||
@@ -227,0 +280,0 @@ */ |
@@ -9,3 +9,2 @@ "use strict"; | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM | ||
@@ -62,2 +61,9 @@ */ | ||
this._propertyMatchingRule = json_convert_enums_1.PropertyMatchingRule.CASE_STRICT; | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties when serializing or deserializing will be ignored, as if they | ||
* were marked optional. | ||
*/ | ||
this._ignoreRequiredCheck = false; | ||
if (operationMode !== undefined && operationMode in json_convert_enums_1.OperationMode) | ||
@@ -80,3 +86,4 @@ this.operationMode = operationMode; | ||
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged | ||
* @returns {number} | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -93,3 +100,4 @@ get: function () { | ||
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged | ||
* @param value | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -112,3 +120,3 @@ set: function (value) { | ||
* | ||
* @returns {number} | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -126,3 +134,3 @@ get: function () { | ||
* | ||
* @param value | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -141,3 +149,3 @@ set: function (value) { | ||
* | ||
* @returns {boolean} | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -151,3 +159,3 @@ get: function () { | ||
* | ||
* @param value | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -167,3 +175,4 @@ set: function (value) { | ||
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive | ||
* @returns {number} | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -179,3 +188,4 @@ get: function () { | ||
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive | ||
* @param value | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
@@ -189,2 +199,28 @@ set: function (value) { | ||
}); | ||
Object.defineProperty(JsonConvert.prototype, "ignoreRequiredCheck", { | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties (undefined) when serializing or deserializing will be | ||
* ignored, as if they were marked optional. | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
get: function () { | ||
return this._ignoreRequiredCheck; | ||
}, | ||
/** | ||
* Determines whether the check for "required" properties should be ignored, making all | ||
* mapped values optional, whether or not the isOptional property mapping parameter is set. | ||
* If true, any missing properties (undefined) when serializing or deserializing will be | ||
* ignored, as if they were marked optional. | ||
* | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
set: function (value) { | ||
this._ignoreRequiredCheck = value; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
//////////////////// | ||
@@ -194,5 +230,11 @@ // PUBLIC METHODS // | ||
/** | ||
* Tries to serialize a TypeScript object or array of objects to JSON. | ||
* Tries to serialize a TypeScript object or array of objects to JSON using the mappings defined on | ||
* the specified class reference. Note that if a class reference is provided, it will be used as | ||
* the source of property mapping for serialization, even if the object or one of its elements is | ||
* an instance of a different class with its own mappings. Also, ONLY the properties from the | ||
* class reference will be serialized - any additional properties on the object(s) will be silently | ||
* ignored. | ||
* | ||
* @param data object or array of objects | ||
* @param classReference the class reference which provides the property mappings to use | ||
* | ||
@@ -203,6 +245,5 @@ * @returns the JSON object | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
JsonConvert.prototype.serialize = function (data) { | ||
JsonConvert.prototype.serialize = function (data, classReference) { | ||
if (this.operationMode === json_convert_enums_1.OperationMode.DISABLE) { | ||
@@ -213,6 +254,6 @@ return data; | ||
if (data instanceof Array) { | ||
return this.serializeArray(data); | ||
return this.serializeArray(data, classReference); | ||
} | ||
else if (typeof data === "object") { // careful: an array is an object in TypeScript! | ||
return this.serializeObject(data); | ||
return this.serializeObject(data, classReference); | ||
} | ||
@@ -226,5 +267,12 @@ else { | ||
/** | ||
* Tries to serialize a TypeScript object to a JSON object. | ||
* Tries to serialize a TypeScript object to a JSON object using either the mappings on the | ||
* provided class reference, if present, or on the provided object. Note that if a class | ||
* reference is provided, it will be used as the source of property mapping for serialization, | ||
* even if the object is itself an instance of a different class with its own mappings. | ||
* Also, ONLY the properties from the class reference will be serialized - any additional | ||
* properties on the object will be silently ignored. | ||
* | ||
* @param instance TypeScript instance | ||
* @param data object containing the values to be mapped to a JSON object, must be an | ||
* instance of a class with JSON mappings if no class reference is provided | ||
* @param classReference optional class reference which provides the property mappings to use | ||
* | ||
@@ -235,11 +283,10 @@ * @returns the JSON object | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
JsonConvert.prototype.serializeObject = function (instance) { | ||
JsonConvert.prototype.serializeObject = function (data, classReference) { | ||
if (this.operationMode === json_convert_enums_1.OperationMode.DISABLE) { | ||
return instance; | ||
return data; | ||
} | ||
// Check if the passed type is allowed | ||
if (instance === undefined) { | ||
if (data === undefined) { | ||
throw new Error("Fatal error in JsonConvert. " + | ||
@@ -249,3 +296,3 @@ "Passed parameter instance in JsonConvert.serializeObject() is undefined. This is not a valid JSON format." + | ||
} | ||
else if (instance === null) { | ||
else if (data === null) { | ||
if (this.valueCheckingMode === json_convert_enums_1.ValueCheckingMode.DISALLOW_NULL) { | ||
@@ -257,6 +304,6 @@ throw new Error("Fatal error in JsonConvert. " + | ||
else { | ||
return instance; | ||
return data; | ||
} | ||
} | ||
else if (typeof (instance) !== "object" || instance instanceof Array) { | ||
else if (typeof (data) !== "object" || data instanceof Array) { | ||
throw new Error("Fatal error in JsonConvert. " + | ||
@@ -270,9 +317,26 @@ "Passed parameter instance in JsonConvert.serializeObject() is not of type object." + | ||
console.log("Receiving JavaScript instance:"); | ||
console.log(instance); | ||
console.log(data); | ||
} | ||
var jsonObject = {}; | ||
// Loop through all initialized class properties | ||
var instance; | ||
if (!!classReference) { | ||
instance = new classReference(); | ||
} | ||
else { | ||
instance = data; | ||
} | ||
// Loop through all initialized class properties on the mapping instance | ||
for (var _i = 0, _a = Object.keys(instance); _i < _a.length; _i++) { | ||
var propertyKey = _a[_i]; | ||
this.serializeObject_loopProperty(instance, propertyKey, jsonObject); | ||
try { | ||
this.serializeObject_loopProperty(data, instance, propertyKey, jsonObject); | ||
} | ||
catch (ex) { | ||
if (this.operationMode === json_convert_enums_1.OperationMode.LOGGING) { | ||
console.log("Failed to serialize property:"); | ||
console.log(ex); | ||
console.log("----------"); | ||
} | ||
throw ex; | ||
} | ||
} | ||
@@ -287,5 +351,12 @@ if (this.operationMode === json_convert_enums_1.OperationMode.LOGGING) { | ||
/** | ||
* Tries to serialize a TypeScript array to a JSON array. | ||
* Tries to serialize a TypeScript array to a JSON array using either the mappings on the | ||
* provided class reference, if present, or on the provided object. Note that if a class | ||
* reference is provided, ALL objects in the array will be serialized using the mappings | ||
* from that class reference, even if they're actually instances of a different class. | ||
* Also, ONLY the properties from the class reference will be serialized - any additional | ||
* properties on the objects will be silently ignored. | ||
* | ||
* @param instanceArray array of TypeScript instances | ||
* @param dataArray array of objects containing the values to be mapped to a JSON object, which | ||
* must be instances of classes with JSON mappings if no class reference is provided | ||
* @param classReference optional class reference which provides the property mappings to use | ||
* | ||
@@ -296,11 +367,10 @@ * @returns the JSON array | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
JsonConvert.prototype.serializeArray = function (instanceArray) { | ||
JsonConvert.prototype.serializeArray = function (dataArray, classReference) { | ||
if (this.operationMode === json_convert_enums_1.OperationMode.DISABLE) { | ||
return instanceArray; | ||
return dataArray; | ||
} | ||
// Check if the passed type is allowed | ||
if (instanceArray === undefined) { | ||
if (dataArray === undefined) { | ||
throw new Error("Fatal error in JsonConvert. " + | ||
@@ -310,3 +380,3 @@ "Passed parameter instanceArray in JsonConvert.serializeArray() is undefined. This is not a valid JSON format." + | ||
} | ||
else if (instanceArray === null) { | ||
else if (dataArray === null) { | ||
if (this.valueCheckingMode === json_convert_enums_1.ValueCheckingMode.DISALLOW_NULL) { | ||
@@ -318,6 +388,6 @@ throw new Error("Fatal error in JsonConvert. " + | ||
else { | ||
return instanceArray; | ||
return dataArray; | ||
} | ||
} | ||
else if (typeof (instanceArray) !== "object" || instanceArray instanceof Array === false) { | ||
else if (typeof (dataArray) !== "object" || dataArray instanceof Array === false) { | ||
throw new Error("Fatal error in JsonConvert. " + | ||
@@ -331,9 +401,9 @@ "Passed parameter instanceArray in JsonConvert.serializeArray() is not of type array." + | ||
console.log("Receiving JavaScript array:"); | ||
console.log(instanceArray); | ||
console.log(dataArray); | ||
} | ||
var jsonArray = []; | ||
// Loop through all array elements | ||
for (var _i = 0, _a = instanceArray; _i < _a.length; _i++) { | ||
var classInstance = _a[_i]; | ||
jsonArray.push(this.serializeObject(classInstance)); | ||
for (var _i = 0, _a = dataArray; _i < _a.length; _i++) { | ||
var dataObject = _a[_i]; | ||
jsonArray.push(this.serializeObject(dataObject, classReference)); | ||
} | ||
@@ -357,3 +427,2 @@ if (this.operationMode === json_convert_enums_1.OperationMode.LOGGING) { | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -388,3 +457,2 @@ */ | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -427,3 +495,13 @@ */ | ||
var propertyKey = _a[_i]; | ||
this.deserializeObject_loopProperty(instance, propertyKey, jsonObject); | ||
try { | ||
this.deserializeObject_loopProperty(instance, propertyKey, jsonObject); | ||
} | ||
catch (ex) { | ||
if (this.operationMode === json_convert_enums_1.OperationMode.LOGGING) { | ||
console.log("Failed to deserialize property:"); | ||
console.log(ex); | ||
console.log("----------"); | ||
} | ||
throw ex; | ||
} | ||
} | ||
@@ -447,3 +525,2 @@ if (this.operationMode === json_convert_enums_1.OperationMode.LOGGING) { | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
@@ -502,11 +579,12 @@ */ | ||
/** | ||
* Tries to find the JSON mapping for a given class property and finally assign the value. | ||
* Tries to find the JSON mapping for a given class property from the given instance used for mapping, | ||
* and finally assign the value from the given dataObject | ||
* | ||
* @param instance the instance of the class | ||
* @param dataObject the object containing the value to be assigned | ||
* @param instance the instance of the class used for mapping | ||
* @param classPropertyName the property name | ||
* @param json the JSON object | ||
* | ||
* @throws throws an Error in case of failure | ||
*/ | ||
JsonConvert.prototype.serializeObject_loopProperty = function (instance, classPropertyName, json) { | ||
JsonConvert.prototype.serializeObject_loopProperty = function (dataObject, instance, classPropertyName, json) { | ||
// Check if a JSON-object mapping is possible for a property | ||
@@ -522,6 +600,6 @@ var mappingOptions = this.getClassPropertyMappingOptions(instance, classPropertyName); | ||
var customConverter = mappingOptions.customConverter; | ||
var classInstancePropertyValue = instance[classPropertyName]; | ||
var classInstancePropertyValue = dataObject[classPropertyName]; | ||
// Check if the class property value exists | ||
if (typeof (classInstancePropertyValue) === "undefined") { | ||
if (isOptional) | ||
if (isOptional || this._ignoreRequiredCheck) | ||
return; | ||
@@ -575,6 +653,7 @@ throw new Error("Fatal error in JsonConvert. " + | ||
} | ||
catch (_a) { } | ||
catch (_a) { | ||
} | ||
// Check if the json value exists | ||
if (typeof (jsonValue) === "undefined") { | ||
if (isOptional) | ||
if (isOptional || this._ignoreRequiredCheck) | ||
return; | ||
@@ -621,12 +700,19 @@ throw new Error("Fatal error in JsonConvert. " + | ||
return null; | ||
// Get direct mapping if possible | ||
var directMappingName = instance[json_convert_options_1.Settings.CLASS_IDENTIFIER] + "." + propertyName; | ||
if (typeof (mappings[directMappingName]) !== "undefined") { | ||
return mappings[directMappingName]; | ||
/* Find mapping by iterating up the prototype chain to find a matching mapping, rather than | ||
* just searching by property name. */ | ||
var prototype = Object.getPrototypeOf(instance); | ||
/* According to documentation, we'll hit null when we've iterated all the way up to the base | ||
* Object, but check for undefined as well in case prototype has been manually set to | ||
* undefined. Note that javascript detects circular prototype references and will cause a | ||
* TypeError, so no need to check for self, the prototype chain will eventually terminate. */ | ||
while (prototype !== null && prototype !== undefined) { | ||
var classIdentifier = prototype[json_convert_options_1.Settings.CLASS_IDENTIFIER]; | ||
if (!!classIdentifier) { | ||
var mappingName = classIdentifier + "." + propertyName; | ||
if (typeof (mappings[mappingName]) !== "undefined") { | ||
return mappings[mappingName]; | ||
} | ||
} | ||
prototype = Object.getPrototypeOf(prototype); | ||
} | ||
// No mapping was found, try to find some | ||
var indirectMappingNames = Object.keys(mappings).filter(function (key) { return key.match("\\." + propertyName + "$"); }); // use endsWidth in later versions | ||
if (indirectMappingNames.length > 0) { | ||
return mappings[indirectMappingNames[0]]; | ||
} | ||
return null; | ||
@@ -663,3 +749,3 @@ }; | ||
if (serialize) | ||
return this.serializeObject(value); | ||
return this.serializeObject(value, expectedJsonType); | ||
else | ||
@@ -822,3 +908,3 @@ return this.deserializeObject(value, expectedJsonType); | ||
} | ||
else if (typeof expectedJsonType === 'function') { | ||
else if (typeof expectedJsonType === "function") { | ||
return (new expectedJsonType()).constructor.name; | ||
@@ -825,0 +911,0 @@ } |
@@ -8,3 +8,2 @@ /** | ||
* | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM$ | ||
@@ -11,0 +10,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132424
1671
598