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.2 to 0.9.3

4

package.json
{
"name": "json2typescript",
"version": "0.9.2",
"version": "0.9.3",
"description": "Provides TypeScript methods to map a JSON string to a JavaScript object on runtime",

@@ -37,4 +37,4 @@ "keywords": [

"type": "git",
"url": ""
"url": "https://github.com/dhlab-basel/json2typescript"
}
}

@@ -26,2 +26,3 @@ # json2typescript

* 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.
* v0.9.2: Added method `serializeString()`, changed `property` names and behaviour

@@ -228,3 +229,3 @@ * v0.9.1: First version released to the public

export class User {
@JsonProperty("jsonKeyOfName", String)
@JsonProperty("jsonKeyOfName", String, false)
public name: string = undefined;

@@ -236,3 +237,3 @@ }

#### First parameter: json key
#### First parameter: jsonKey

@@ -244,5 +245,6 @@ The first parameter of `@JsonProperty` is the JSON object key.

#### Second parameter: expected type
#### Second parameter (optional): expectedType
The second parameter of `@JsonProperty` is the expected type.
The second parameter of `@JsonProperty` is the expected type.
This parameter is optional; the default value is undefined (which allows any type).
Make sure you pass the class name and not an instance of the class.

@@ -270,2 +272,10 @@ In case of primitive types, you have to use the upper case names.

#### Third parameter (optional): isOptional
The third parameter of `@JsonProperty` determines whether the `jsonKey` has to be present in the json.
This parameter is optional; the default value is false.
By default, `JsonConvert` throws an exception if a decorated class property cannot be found in the given JSON.
If you set the third parameter to true, there is no exception when it is missing.
The type is still checked as soon the property is present again.
#### Important notes

@@ -307,3 +317,2 @@

`(bool) JsonConvert.valueCheckingMode`

@@ -319,3 +328,3 @@

> The TypeScript developer team suggests you to avoid null values. If your JSON api doesn't return null values, you should try the last flag disallowing null values.
> Tip: The TypeScript developer team suggests you to avoid null values. If your JSON api doesn't return null values, you should try the last flag disallowing null values.

@@ -322,0 +331,0 @@ ### Public methods

/**
* 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.2
* @version 0.9.3
* @licence MIT

@@ -166,2 +166,3 @@ * @see https://www.npmjs.com/package/json2typescript full documentation

let expectedType: any = mapping[propertyKey]["type"];
let isOptional: boolean = mapping[propertyKey]["optional"];

@@ -173,2 +174,5 @@ let jsonValue: any = json[jsonKey];

if (typeof(jsonValue) === "undefined") {
if (isOptional) return;
throw new Error(

@@ -309,2 +313,33 @@ "Fatal error in JsonConvert. " +

// Check if attempt was 1-d and expected was n-d
if (expectedType instanceof Array && jsonValue instanceof Object) {
let array = [];
// No data given, so return empty value
if (jsonValue.length === 0) {
return array;
}
// We obviously don't care about the type, so return the json value as is
if (expectedType.length === 0) {
return jsonValue;
}
// Loop through the data. Both expectedType and jsonValue are at least of length 1
let autofillType: boolean = expectedType.length < Object.keys(jsonValue).length;
let i = 0;
for (let key in jsonValue) {
if (autofillType && i >= expectedType.length) expectedType[i] = expectedType[i-1];
array[key] = JsonConvert.deserializeObject_mapProperty(expectedType[i], jsonValue[key]);
i++;
}
return array;
}
// Check if attempt was 1-d and expected was n-d
if (expectedType instanceof Array) {

@@ -402,9 +437,10 @@ if (jsonValue === null) {

* Custom type: YourClassName
* Array type: [String|Numer|Boolean|YourClassName]
* Array type: [String|Number|Boolean|YourClassName]
* @param jsonKey the key in the expected JSON object
* @param type the expected type String|Boolean|Number|any
* @param expectedType optional param (default: undefined), the expected type String|Boolean|Number|any
* @param isOptional optional param (default: false), if true, the property does not have to be present in the json object
* @see https://www.npmjs.com/package/json2typescript full documentation
* @returns {(target:any, key:string)=>void}
*/
export function JsonProperty(jsonKey: string, type?: any): any {
export function JsonProperty(jsonKey: string, expectedType?: any, isOptional?: boolean): any {

@@ -417,8 +453,14 @@ return function (target: any, key: string): void {

if (typeof(isOptional) === "undefined") {
isOptional = false;
}
target["__jsonconvert__mapping__"][key] = {
"jsonKey": jsonKey,
"type": type
"type": expectedType,
"optional": isOptional
};
}
}

@@ -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