json2typescript
Advanced tools
Comparing version 0.9.4 to 0.9.5
{ | ||
"name": "json2typescript", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"description": "Provides TypeScript methods to map a JSON string to a JavaScript object on runtime", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -26,2 +26,3 @@ # json2typescript | ||
* v0.9.5: New method for deserializing array of objects. | ||
* v0.9.4: Class properties are now not overridden to `undefined` if there is no decorator and no matching json value. | ||
@@ -28,0 +29,0 @@ * v0.9.3: It is now possible to map an JSON object to an TypeScript array, then the object keys become the array keys. Also, class properties can be set to optional. See below in the chapter "decorators" for more information. |
/** | ||
* Offers a simple API for mapping json objects to TypeScript/JavaScript classes and vice versa. | ||
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland | ||
* @version 0.9.4 | ||
* @version 0.9.5 | ||
* @licence MIT | ||
@@ -102,2 +102,3 @@ * @see https://www.npmjs.com/package/json2typescript full documentation | ||
return JsonConvert.deserializeObject(jsonObject, classObject); | ||
@@ -117,3 +118,3 @@ | ||
if (typeof(jsonObject) !== "object") { | ||
if (typeof(jsonObject) !== "object" || jsonObject instanceof Array) { | ||
throw new Error( | ||
@@ -132,3 +133,3 @@ "Fatal error in JsonConvert. " + | ||
// Loop through all (not undefined) class properties | ||
// Loop through all initialized class properties | ||
for (const propertyKey of Object.keys(classInstance)) { | ||
@@ -144,5 +145,44 @@ JsonConvert.deserializeObject_loopProperty(classInstance, propertyKey, jsonObject); | ||
return classInstance; | ||
} | ||
/** | ||
* Tries to deserialize a JSON array to a TypeScript class. | ||
* @param jsonArray the JSON array | ||
* @param classObject the object class | ||
* @returns {any[]} the deserialized array of object instances | ||
* @throws an exception in case of failure | ||
* @see https://www.npmjs.com/package/json2typescript full documentation | ||
*/ | ||
public static deserializeArray(jsonArray: any[], classObject: { new(): any }): any[] { | ||
if (typeof(jsonArray) !== "object" || jsonArray instanceof Array === false) { | ||
throw new Error( | ||
"Fatal error in JsonConvert. " + | ||
"Passed parameter jsonArray in JsonConvert.deserializeArray() is not of type array object." | ||
); | ||
} | ||
if (JsonConvert.debugMode) { | ||
console.log("Receiving JSON array:"); | ||
console.log(jsonArray); | ||
} | ||
let array: any[] = []; | ||
// Loop through all array elements | ||
for (const jsonObject of jsonArray) { | ||
array.push(JsonConvert.deserializeObject(jsonObject, classObject)); | ||
} | ||
if (JsonConvert.debugMode) { | ||
console.log("Returning array of CLASS instances:"); | ||
console.log(array); | ||
} | ||
return array; | ||
} | ||
/** | ||
* Tries to find the JSON mapping for a given class property and finally assign the value. | ||
@@ -149,0 +189,0 @@ * @param classInstance the instance of the class |
@@ -0,0 +0,0 @@ { |
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
36587
416
412