ts-serializable 2.0.52 → 2.0.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/Serializable.d.ts +1 -1
- package/dist/classes/Serializable.js +6 -12
- package/dist/classes/Serializable.mjs +6 -12
- package/dist/decorators/JsonIgnore.js +0 -1
- package/dist/decorators/JsonIgnore.mjs +0 -1
- package/dist/decorators/JsonName.js +0 -1
- package/dist/decorators/JsonName.mjs +0 -1
- package/dist/decorators/JsonObject.js +0 -7
- package/dist/decorators/JsonObject.mjs +0 -7
- package/dist/decorators/JsonProperty.js +0 -1
- package/dist/decorators/JsonProperty.mjs +0 -1
- package/dist/models/AcceptedType.d.ts +1 -1
- package/dist/models/AcceptedType.js +3 -0
- package/dist/models/AcceptedType.mjs +3 -0
- package/package.json +6 -6
- package/src/classes/Serializable.ts +11 -19
- package/src/decorators/JsonIgnore.ts +0 -2
- package/src/decorators/JsonName.ts +0 -3
- package/src/decorators/JsonObject.ts +3 -6
- package/src/decorators/JsonProperty.ts +0 -2
- package/src/models/AcceptedType.ts +2 -2
|
@@ -44,7 +44,7 @@ export declare class Serializable {
|
|
|
44
44
|
* @returns {object}
|
|
45
45
|
* @memberof Serializable
|
|
46
46
|
*/
|
|
47
|
-
toJSON():
|
|
47
|
+
toJSON(): Record<string, unknown>;
|
|
48
48
|
/**
|
|
49
49
|
* Process exceptions from wrong types.
|
|
50
50
|
* By default just print warning in console, but can by override for drop exception or logging to backend.
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable
|
|
2
|
+
/* eslint-disable no-prototype-builtins */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
4
4
|
/* eslint-disable complexity */
|
|
5
5
|
/* eslint-disable max-lines-per-function */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
|
-
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
|
|
8
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
9
6
|
/* eslint-disable max-statements */
|
|
10
|
-
/* eslint-disable @typescript-eslint/no-unsafe-
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.Serializable = void 0;
|
|
13
10
|
const SerializationSettings_1 = require("../models/SerializationSettings");
|
|
@@ -81,12 +78,10 @@ class Serializable {
|
|
|
81
78
|
for (const prop in fromJson) {
|
|
82
79
|
// Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods
|
|
83
80
|
if (fromJson.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
|
|
84
|
-
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop)
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop) !== true) {
|
|
82
|
+
const toProp = this.getJsonPropertyName(prop);
|
|
83
|
+
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
87
84
|
}
|
|
88
|
-
const toProp = this.getJsonPropertyName(prop);
|
|
89
|
-
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
90
85
|
}
|
|
91
86
|
}
|
|
92
87
|
return toJson;
|
|
@@ -180,12 +175,11 @@ class Serializable {
|
|
|
180
175
|
acceptedType !== void 0 &&
|
|
181
176
|
!Array.isArray(acceptedType) &&
|
|
182
177
|
(acceptedType.prototype instanceof Serializable ||
|
|
183
|
-
Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType)) &&
|
|
178
|
+
Boolean(Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType))) &&
|
|
184
179
|
jsonValue !== null &&
|
|
185
180
|
jsonValue !== void 0 &&
|
|
186
181
|
typeof jsonValue === "object" && !Array.isArray(jsonValue)) {
|
|
187
182
|
const TypeConstructor = acceptedType;
|
|
188
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
189
183
|
return new TypeConstructor().fromJSON(jsonValue, settings);
|
|
190
184
|
}
|
|
191
185
|
else if ( // Instance any other class, not Serializable, for parse from other classes instance
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
/* eslint-disable
|
|
1
|
+
/* eslint-disable no-prototype-builtins */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
3
3
|
/* eslint-disable complexity */
|
|
4
4
|
/* eslint-disable max-lines-per-function */
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
6
|
-
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
|
|
7
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
8
5
|
/* eslint-disable max-statements */
|
|
9
|
-
/* eslint-disable @typescript-eslint/no-unsafe-
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
10
7
|
import { SerializationSettings } from "../models/SerializationSettings";
|
|
11
8
|
/**
|
|
12
9
|
* Class how help you deserialize object to classes.
|
|
@@ -78,12 +75,10 @@ export class Serializable {
|
|
|
78
75
|
for (const prop in fromJson) {
|
|
79
76
|
// Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods
|
|
80
77
|
if (fromJson.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
|
|
81
|
-
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop)
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop) !== true) {
|
|
79
|
+
const toProp = this.getJsonPropertyName(prop);
|
|
80
|
+
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
84
81
|
}
|
|
85
|
-
const toProp = this.getJsonPropertyName(prop);
|
|
86
|
-
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
87
82
|
}
|
|
88
83
|
}
|
|
89
84
|
return toJson;
|
|
@@ -177,12 +172,11 @@ export class Serializable {
|
|
|
177
172
|
acceptedType !== void 0 &&
|
|
178
173
|
!Array.isArray(acceptedType) &&
|
|
179
174
|
(acceptedType.prototype instanceof Serializable ||
|
|
180
|
-
Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType)) &&
|
|
175
|
+
Boolean(Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType))) &&
|
|
181
176
|
jsonValue !== null &&
|
|
182
177
|
jsonValue !== void 0 &&
|
|
183
178
|
typeof jsonValue === "object" && !Array.isArray(jsonValue)) {
|
|
184
179
|
const TypeConstructor = acceptedType;
|
|
185
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
186
180
|
return new TypeConstructor().fromJSON(jsonValue, settings);
|
|
187
181
|
}
|
|
188
182
|
else if ( // Instance any other class, not Serializable, for parse from other classes instance
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
3
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
4
|
-
/* eslint-disable max-statements */
|
|
5
|
-
/* eslint-disable @typescript-eslint/unbound-method */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.jsonObject = void 0;
|
|
11
4
|
const Serializable_1 = require("../classes/Serializable");
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
3
|
-
/* eslint-disable max-statements */
|
|
4
|
-
/* eslint-disable @typescript-eslint/unbound-method */
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
8
1
|
import { Serializable } from "../classes/Serializable";
|
|
9
2
|
export const jsonObject = (settings, extend) => (target) => {
|
|
10
3
|
if (extend === true) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare type AcceptedType = null | void | BooleanConstructor | NumberConstructor | StringConstructor | ObjectConstructor | (new (...args:
|
|
1
|
+
export declare type AcceptedType = null | void | BooleanConstructor | NumberConstructor | StringConstructor | ObjectConstructor | (new (...args: unknown[]) => object) | DateConstructor | SymbolConstructor;
|
|
2
2
|
interface IRecursiveArray<T> extends Array<IRecursiveArray<T> | T> {
|
|
3
3
|
}
|
|
4
4
|
export declare type AcceptedTypes = AcceptedType | IRecursiveArray<AcceptedType>;
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/sort-type-union-intersection-members */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-invalid-void-type */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-type-alias */
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-serializable",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.58",
|
|
4
4
|
"description": "Serialization and deserializtion for classes",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"typings": "./dist/index.d.ts",
|
|
8
8
|
"dependencies": {},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"@labeg/code-style": "^2.0.
|
|
11
|
-
"@types/chai": "^4.3.
|
|
12
|
-
"@types/mocha": "^9.1.
|
|
10
|
+
"@labeg/code-style": "^2.0.34",
|
|
11
|
+
"@types/chai": "^4.3.1",
|
|
12
|
+
"@types/mocha": "^9.1.1",
|
|
13
13
|
"chai": "^4.3.6",
|
|
14
|
-
"mocha": "^
|
|
14
|
+
"mocha": "^10.0.0",
|
|
15
15
|
"reflect-metadata": "^0.1.13",
|
|
16
16
|
"ts-node": "^10.7.0",
|
|
17
|
-
"typescript": "^4.6.
|
|
17
|
+
"typescript": "^4.6.4"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"cs:eslint": "eslint --fix -c .eslintrc.js --ext .tsx,.ts,.jsx,.js ./src/ ./tests/",
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
/* eslint-disable
|
|
1
|
+
/* eslint-disable no-prototype-builtins */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
3
3
|
/* eslint-disable complexity */
|
|
4
4
|
/* eslint-disable max-lines-per-function */
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
6
|
-
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
|
|
7
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
8
5
|
/* eslint-disable max-statements */
|
|
9
|
-
/* eslint-disable @typescript-eslint/no-unsafe-
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
10
7
|
|
|
11
8
|
import type {AcceptedTypes} from "../models/AcceptedType";
|
|
12
9
|
import {SerializationSettings} from "../models/SerializationSettings";
|
|
@@ -104,21 +101,17 @@ export class Serializable {
|
|
|
104
101
|
* @returns {object}
|
|
105
102
|
* @memberof Serializable
|
|
106
103
|
*/
|
|
107
|
-
public toJSON ():
|
|
108
|
-
const fromJson:
|
|
109
|
-
const toJson:
|
|
104
|
+
public toJSON (): Record<string, unknown> {
|
|
105
|
+
const fromJson: this = {...this};
|
|
106
|
+
const toJson: Record<string, unknown> = {};
|
|
110
107
|
|
|
111
108
|
for (const prop in fromJson) {
|
|
112
109
|
// Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods
|
|
113
110
|
if (fromJson.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
|
|
114
|
-
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop)
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, prop) !== true) {
|
|
112
|
+
const toProp = this.getJsonPropertyName(prop);
|
|
113
|
+
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
117
114
|
}
|
|
118
|
-
|
|
119
|
-
const toProp = this.getJsonPropertyName(prop);
|
|
120
|
-
|
|
121
|
-
Reflect.set(toJson, toProp, Reflect.get(fromJson, prop));
|
|
122
115
|
}
|
|
123
116
|
}
|
|
124
117
|
|
|
@@ -187,7 +180,7 @@ export class Serializable {
|
|
|
187
180
|
acceptedType === Object &&
|
|
188
181
|
(typeof jsonValue === "object")
|
|
189
182
|
) {
|
|
190
|
-
return Object(jsonValue)
|
|
183
|
+
return Object(jsonValue);
|
|
191
184
|
} else if (// Date
|
|
192
185
|
acceptedType === Date &&
|
|
193
186
|
(typeof jsonValue === "string" || jsonValue instanceof String || jsonValue instanceof Date)
|
|
@@ -227,7 +220,7 @@ export class Serializable {
|
|
|
227
220
|
!Array.isArray(acceptedType) &&
|
|
228
221
|
(
|
|
229
222
|
acceptedType.prototype instanceof Serializable ||
|
|
230
|
-
Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType)
|
|
223
|
+
Boolean(Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType))
|
|
231
224
|
) &&
|
|
232
225
|
jsonValue !== null &&
|
|
233
226
|
jsonValue !== void 0 &&
|
|
@@ -235,7 +228,6 @@ export class Serializable {
|
|
|
235
228
|
) {
|
|
236
229
|
const TypeConstructor: new () => Serializable = acceptedType as new () => Serializable;
|
|
237
230
|
|
|
238
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
239
231
|
return new TypeConstructor().fromJSON(jsonValue, settings);
|
|
240
232
|
} else if (// Instance any other class, not Serializable, for parse from other classes instance
|
|
241
233
|
acceptedType instanceof Function &&
|
|
@@ -248,7 +240,7 @@ export class Serializable {
|
|
|
248
240
|
// Process wrong type and return default value
|
|
249
241
|
this.onWrongType(prop, "is invalid", jsonValue);
|
|
250
242
|
|
|
251
|
-
return Reflect.get(this, prop)
|
|
243
|
+
return Reflect.get(this, prop);
|
|
252
244
|
}
|
|
253
245
|
|
|
254
246
|
protected getJsonPropertyName (thisProperty: string, settings?: Partial<SerializationSettings>): string {
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
3
|
-
/* eslint-disable max-statements */
|
|
4
|
-
/* eslint-disable @typescript-eslint/unbound-method */
|
|
5
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
6
3
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
|
-
/* eslint-disable @typescript-eslint/
|
|
8
|
-
|
|
4
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
5
|
+
/* eslint-disable max-statements */
|
|
9
6
|
import type {SerializationSettings} from "../models/SerializationSettings";
|
|
10
7
|
import {Serializable} from "../classes/Serializable";
|
|
11
8
|
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
1
|
/* eslint-disable @typescript-eslint/sort-type-union-intersection-members */
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-invalid-void-type */
|
|
4
3
|
/* eslint-disable @typescript-eslint/no-type-alias */
|
|
4
|
+
|
|
5
5
|
export type AcceptedType = null |
|
|
6
6
|
void |
|
|
7
7
|
BooleanConstructor |
|
|
8
8
|
NumberConstructor |
|
|
9
9
|
StringConstructor |
|
|
10
10
|
ObjectConstructor |
|
|
11
|
-
(new (...args:
|
|
11
|
+
(new (...args: unknown[]) => object) |
|
|
12
12
|
// Extended deserialization
|
|
13
13
|
DateConstructor |
|
|
14
14
|
SymbolConstructor; // Add ArrayBufferConstructor, MapConstructor, RegExpConstructor and many others...
|