Skip to content

Commit dc95c1a

Browse files
Adjusted code formatting, documentation
1 parent 0e0481d commit dc95c1a

6 files changed

Lines changed: 65 additions & 54 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# v1.3.0 (2020-04-13)
2+
3+
## Bug Fixes
4+
5+
* Fixed content of `package-lock.json`, closes #125
6+
7+
## Features
8+
9+
* Implemented `ignoreRequiredCheck` flag and allow serialization without having instances, closes [#126](https://github.com/AppVision-GmbH/json2typescript/pull/126)
10+
11+
## Breaking Changes
12+
13+
* 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.
14+
115
# v1.2.5 (2020-03-01)
216

317
## Bug Fixes

‎LICENSE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2018 Andreas Aeschlimann
1+
Copyright (c) 2020 Andreas Aeschlimann
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1616
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1717
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19-
SOFTWARE.
19+
SOFTWARE.

‎README.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ The type is still checked as soon the property is present again.
359359
The same applies for the case when you try to serialize a TypeScript object to a JSON object. If the property is not defined in the class and optional, it will not be added to the JSON object.
360360
361361
> Tip: Some API's return null instead of omitting optional values.
362-
If you flag a property as optional, `json2typescript` will ignore null values and keep the default value of the property instead.
362+
If you flag a property as optional, `json2typescript` will ignore (JSON) null values at deserialization and keep the default value of the property instead.
363363
This fact is particularly helpful if your project uses TypeScript `strictNullChecks` or/and disallows `null` values through the `valueCheckingMode` property in `json2typescript`.
364+
Furthermore, note that the instance property `ignoreRequiredCheck` – if set to true – overrides all `isOptional` values for all properties.
364365
365366
#### Important notes
366367
@@ -482,13 +483,23 @@ You may assign the following two values:
482483

483484
The default is `PropertyMatchingRule.CASE_STRICT`.
484485

486+
#### Ignore required property checks
487+
488+
`(bool) JsonConvertignoreRequiredCheck`
489+
490+
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.
491+
If true, any missing properties (undefined) when serializing or deserializing will be ignored, as if they were marked optional.
492+
Note that properties explicitly set to null will be unaffected by this flag – they will be ignored if optional and included if not.
493+
494+
The default is `false`.
495+
485496
### Public methods
486497

487498
`json2typescript` allows you to map JSON objects (or arrays) to TypeScript objects (or arrays) and vice versa.
488499

489500
#### Serializing (TypeScript to JSON)
490501

491-
`(any) serialize(data: T | T[])`
502+
`(any) serialize(data: any | any[])`
492503

493504
Tries to serialize a TypeScript object or array of objects to JSON.
494505

@@ -506,8 +517,8 @@ Tries to deserialize given JSON to a TypeScript object or array of objects.
506517
507518
The methods `serialize()` and `deserialize()` will automatically detect the dimension of your param (either object or array).
508519
In case you would like to force `json2typescript` to use a specific way, you can use the following methods instead:
509-
- `(any) serializeObject(instance: T)`
510-
- `(any[]) serializeArray(instanceArray: T[])`
520+
- `(any) serializeObject(instance: any)`
521+
- `(any[]) serializeArray(instanceArray: any[])`
511522
- `(T) deserializeObject(jsonObject: any, classReference: { new(): T })`
512523
- `(T[]) deserializeArray(jsonArray: any[], classReference: { new(): T })`
513524

‎src/json2typescript/json-convert-enums.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* - ENABLE: json2typescript is enabled, but only errors are logged
77
* - LOGGING: json2typescript is enabled and detailed information is logged
88
*
9-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
109
* @see https://www.npmjs.com/package/json2typescript full documentation
1110
*/
1211
export enum OperationMode {
@@ -22,7 +21,6 @@ export enum OperationMode {
2221
* - CASE_STRICT: JSON properties need to match exactly the names in the decorators
2322
* - CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
2423
*
25-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
2624
* @see https://www.npmjs.com/package/json2typescript full documentation
2725
*/
2826
export enum PropertyMatchingRule {
@@ -38,11 +36,10 @@ export enum PropertyMatchingRule {
3836
* - ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
3937
* - DISALLOW_NULL: no null values are tolerated in the JSON
4038
*
41-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
4239
* @see https://www.npmjs.com/package/json2typescript full documentation
4340
*/
4441
export enum ValueCheckingMode {
4542
ALLOW_NULL = 1,
4643
ALLOW_OBJECT_NULL = 2,
4744
DISALLOW_NULL = 3
48-
};
45+
};

‎src/json2typescript/json-convert.ts‎

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Any } from "./any";
55
/**
66
* Offers a simple API for mapping JSON objects to TypeScript/JavaScript classes and vice versa.
77
*
8-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
98
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM
109
*/
1110
export class JsonConvert {
@@ -32,7 +31,8 @@ export class JsonConvert {
3231
* - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
3332
* - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
3433
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
35-
* @returns {number}
34+
*
35+
* @see https://www.npmjs.com/package/json2typescript full documentation
3636
*/
3737
get operationMode(): number {
3838
return this._operationMode;
@@ -45,7 +45,8 @@ export class JsonConvert {
4545
* - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
4646
* - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
4747
* - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
48-
* @param value
48+
*
49+
* @see https://www.npmjs.com/package/json2typescript full documentation
4950
*/
5051
set operationMode(value: number) {
5152
if (value in OperationMode) this._operationMode = value;
@@ -69,7 +70,7 @@ export class JsonConvert {
6970
* - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
7071
* - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON
7172
*
72-
* @returns {number}
73+
* @see https://www.npmjs.com/package/json2typescript full documentation
7374
*/
7475
get valueCheckingMode(): number {
7576
return this._valueCheckingMode;
@@ -83,7 +84,7 @@ export class JsonConvert {
8384
* - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
8485
* - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON
8586
*
86-
* @param value
87+
* @see https://www.npmjs.com/package/json2typescript full documentation
8788
*/
8889
set valueCheckingMode(value: number) {
8990
if (value in ValueCheckingMode) this._valueCheckingMode = value;
@@ -99,7 +100,7 @@ export class JsonConvert {
99100
* Determines whether primitive types should be checked.
100101
* If true, it will be allowed to assign primitive to other primitive types.
101102
*
102-
* @returns {boolean}
103+
* @see https://www.npmjs.com/package/json2typescript full documentation
103104
*/
104105
get ignorePrimitiveChecks(): boolean {
105106
return this._ignorePrimitiveChecks;
@@ -109,7 +110,7 @@ export class JsonConvert {
109110
* Determines whether primitive types should be checked.
110111
* If true, it will be allowed to assign primitive to other primitive types.
111112
*
112-
* @param value
113+
* @see https://www.npmjs.com/package/json2typescript full documentation
113114
*/
114115
set ignorePrimitiveChecks(value: boolean) {
115116
this._ignorePrimitiveChecks = value;
@@ -130,7 +131,8 @@ export class JsonConvert {
130131
* You may assign the following values:
131132
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
132133
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
133-
* @returns {number}
134+
*
135+
* @see https://www.npmjs.com/package/json2typescript full documentation
134136
*/
135137
get propertyMatchingRule(): number {
136138
return this._propertyMatchingRule;
@@ -142,47 +144,42 @@ export class JsonConvert {
142144
* You may assign the following values:
143145
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
144146
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
145-
* @param value
147+
*
148+
* @see https://www.npmjs.com/package/json2typescript full documentation
146149
*/
147150
set propertyMatchingRule(value: number) {
148151
if (value in PropertyMatchingRule) this._propertyMatchingRule = value;
149152
}
150153

151154
/**
152-
* Determines whether the check for "required" properties should be ignored, making ALL
155+
* Determines whether the check for "required" properties should be ignored, making all
153156
* mapped values optional, whether or not the isOptional property mapping parameter is set.
154157
* If true, any missing properties when serializing or deserializing will be ignored, as if they
155-
* were marked optional. Defaults to false.
158+
* were marked optional.
156159
*/
157-
private _ignoreRequiredCheck = false;
160+
private _ignoreRequiredCheck: boolean = false;
158161

159162
/**
160-
* Determines whether the check for "required" properties should be ignored, making ALL
163+
* Determines whether the check for "required" properties should be ignored, making all
161164
* mapped values optional, whether or not the isOptional property mapping parameter is set.
162165
* If true, any missing properties (undefined) when serializing or deserializing will be
163-
* ignored, as if they were marked optional. Note that properties explicitly set to null
164-
* will be unaffected by this flag - they will be ignored if optional and included if not.
165-
* Defaults to false.
166-
* @returns {boolean} true if all mapped properties will be considered optional, even if the mapping
167-
* doesn't explicitly specify this, false if the mapping will determine whether
168-
* a property is optional or not (default)
166+
* ignored, as if they were marked optional.
167+
*
168+
* @see https://www.npmjs.com/package/json2typescript full documentation
169169
*/
170170
get ignoreRequiredCheck(): boolean {
171171
return this._ignoreRequiredCheck;
172172
}
173173

174174
/**
175-
* Determines whether the check for "required" properties should be ignored, making ALL
175+
* Determines whether the check for "required" properties should be ignored, making all
176176
* mapped values optional, whether or not the isOptional property mapping parameter is set.
177177
* If true, any missing properties (undefined) when serializing or deserializing will be
178-
* ignored, as if they were marked optional. Note that properties explicitly set to null
179-
* will be unaffected by this flag - they will be ignored if optional and included if not.
180-
* Defaults to false.
181-
* @param value true if all mapped properties will be considered optional, even if the mapping
182-
* doesn't explicitly specify this, false if the mapping will determine whether a
183-
* property is optional or not (default)
178+
* ignored, as if they were marked optional.
179+
*
180+
* @see https://www.npmjs.com/package/json2typescript full documentation
184181
*/
185-
set ignoreRequiredCheck( value: boolean) {
182+
set ignoreRequiredCheck(value: boolean) {
186183
this._ignoreRequiredCheck = value;
187184
}
188185

@@ -229,7 +226,6 @@ export class JsonConvert {
229226
*
230227
* @throws an Error in case of failure
231228
*
232-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
233229
* @see https://www.npmjs.com/package/json2typescript full documentation
234230
*/
235231
serialize<T>(data: any | any[], classReference?: { new(): T }): any | any[] {
@@ -245,9 +241,9 @@ export class JsonConvert {
245241
return this.serializeObject(data, classReference);
246242
} else {
247243
throw new Error(
248-
"Fatal error in JsonConvert. " +
249-
"Passed parameter data in JsonConvert.serialize() is not in valid format (object or array)." +
250-
"\n"
244+
"Fatal error in JsonConvert. " +
245+
"Passed parameter data in JsonConvert.serialize() is not in valid format (object or array)." +
246+
"\n"
251247
);
252248
}
253249
}
@@ -268,7 +264,6 @@ export class JsonConvert {
268264
*
269265
* @throws an Error in case of failure
270266
*
271-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
272267
* @see https://www.npmjs.com/package/json2typescript full documentation
273268
*/
274269
serializeObject<T>(data: any, classReference?: { new(): T }): any {
@@ -327,8 +322,7 @@ export class JsonConvert {
327322
for (const propertyKey of Object.keys(instance as any)) {
328323
try {
329324
this.serializeObject_loopProperty(data, instance, propertyKey, jsonObject);
330-
}
331-
catch (ex) {
325+
} catch (ex) {
332326
if (this.operationMode === OperationMode.LOGGING) {
333327
console.log("Failed to serialize property:");
334328
console.log(ex);
@@ -364,7 +358,6 @@ export class JsonConvert {
364358
*
365359
* @throws an Error in case of failure
366360
*
367-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
368361
* @see https://www.npmjs.com/package/json2typescript full documentation
369362
*/
370363
serializeArray<T>(dataArray: any[], classReference?: { new(): T }): any[] {
@@ -414,7 +407,7 @@ export class JsonConvert {
414407
let jsonArray: any[] = [];
415408

416409
// Loop through all array elements
417-
for (const dataObject of <any> dataArray) {
410+
for (const dataObject of <any>dataArray) {
418411
jsonArray.push(this.serializeObject(dataObject, classReference));
419412
}
420413

@@ -438,7 +431,6 @@ export class JsonConvert {
438431
*
439432
* @throws an Error in case of failure
440433
*
441-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
442434
* @see https://www.npmjs.com/package/json2typescript full documentation
443435
*/
444436
deserialize<T>(json: any, classReference: { new(): T }): T | T[] {
@@ -472,7 +464,6 @@ export class JsonConvert {
472464
*
473465
* @throws an Error in case of failure
474466
*
475-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
476467
* @see https://www.npmjs.com/package/json2typescript full documentation
477468
*/
478469
deserializeObject<T>(jsonObject: any, classReference: { new(): T }): T {
@@ -525,8 +516,7 @@ export class JsonConvert {
525516
for (const propertyKey of Object.keys(instance as any)) {
526517
try {
527518
this.deserializeObject_loopProperty(instance, propertyKey, jsonObject);
528-
}
529-
catch (ex) {
519+
} catch (ex) {
530520
if (this.operationMode === OperationMode.LOGGING) {
531521
console.log("Failed to deserialize property:");
532522
console.log(ex);
@@ -556,7 +546,6 @@ export class JsonConvert {
556546
*
557547
* @throws an Error in case of failure
558548
*
559-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
560549
* @see https://www.npmjs.com/package/json2typescript full documentation
561550
*/
562551
deserializeArray<T>(jsonArray: any[], classReference: { new(): T }): T[] {
@@ -719,7 +708,8 @@ export class JsonConvert {
719708
let jsonValue: any = undefined;
720709
try {
721710
jsonValue = this.getObjectValue(json, jsonPropertyName);
722-
} catch {}
711+
} catch {
712+
}
723713

724714

725715
// Check if the json value exists
@@ -1024,7 +1014,7 @@ export class JsonConvert {
10241014
return "any";
10251015
} else if (expectedJsonType === String || expectedJsonType === Boolean || expectedJsonType === Number) {
10261016
return (new expectedJsonType()).constructor.name.toLowerCase();
1027-
} else if (typeof expectedJsonType === 'function') {
1017+
} else if (typeof expectedJsonType === "function") {
10281018
return (new expectedJsonType()).constructor.name;
10291019
} else if (expectedJsonType === undefined) {
10301020
return "undefined"

‎src/json2typescript/json-custom-convert.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* - serialize() to map a TypeScript object to a JSON object
66
* - deserialize() to map a JSON object to a TypeScript object
77
*
8-
* @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
98
* @see https://www.npmjs.com/package/json2typescript full documentation on NPM$
109
*/
1110
export interface JsonCustomConvert<T> {

0 commit comments

Comments
 (0)