Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json2typescript

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json2typescript - npm Package Compare versions

Comparing version 0.9.4 to 0.9.5

2

package.json
{
"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 @@ {

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc