ts-serializable 2.0.68 → 3.0.2
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 +2 -2
- package/dist/classes/Serializable.js +9 -14
- package/dist/decorators/JsonIgnore.js +1 -5
- package/dist/decorators/JsonName.js +1 -5
- package/dist/decorators/JsonObject.d.ts +1 -1
- package/dist/decorators/JsonObject.js +9 -13
- package/dist/decorators/JsonProperty.d.ts +1 -1
- package/dist/decorators/JsonProperty.js +1 -5
- package/dist/enums/DateFormatHandling.js +2 -5
- package/dist/enums/DefaultValueHandling.js +2 -5
- package/dist/enums/LogLevels.js +2 -5
- package/dist/enums/MissingMemberHandling.js +2 -5
- package/dist/enums/NullValueHandling.js +2 -5
- package/dist/enums/ReferenceLoopHandling.js +2 -5
- package/dist/index.d.ts +16 -16
- package/dist/index.js +15 -33
- package/dist/models/AcceptedType.d.ts +2 -2
- package/dist/models/AcceptedType.js +1 -3
- package/dist/models/SerializationSettings.d.ts +7 -7
- package/dist/models/SerializationSettings.js +13 -17
- package/dist/naming-strategies/INamingStrategy.js +1 -2
- package/dist/naming-strategies/KebabCaseNamingStrategy.d.ts +1 -1
- package/dist/naming-strategies/KebabCaseNamingStrategy.js +1 -5
- package/dist/naming-strategies/PascalCaseNamingStrategy.d.ts +1 -1
- package/dist/naming-strategies/PascalCaseNamingStrategy.js +1 -5
- package/dist/naming-strategies/SnakeCaseNamingStrategy.d.ts +1 -1
- package/dist/naming-strategies/SnakeCaseNamingStrategy.js +1 -5
- package/package.json +11 -14
- package/src/classes/Serializable.ts +2 -2
- package/src/decorators/JsonObject.ts +2 -2
- package/src/decorators/JsonProperty.ts +1 -1
- package/src/index.ts +16 -16
- package/src/models/AcceptedType.ts +13 -11
- package/src/models/SerializationSettings.ts +7 -7
- package/src/naming-strategies/KebabCaseNamingStrategy.ts +1 -1
- package/src/naming-strategies/PascalCaseNamingStrategy.ts +1 -1
- package/src/naming-strategies/SnakeCaseNamingStrategy.ts +1 -1
- package/dist/classes/Serializable.mjs +0 -218
- package/dist/decorators/JsonIgnore.mjs +0 -3
- package/dist/decorators/JsonName.mjs +0 -3
- package/dist/decorators/JsonObject.mjs +0 -16
- package/dist/decorators/JsonProperty.mjs +0 -3
- package/dist/enums/DateFormatHandling.mjs +0 -5
- package/dist/enums/DefaultValueHandling.mjs +0 -7
- package/dist/enums/LogLevels.mjs +0 -10
- package/dist/enums/MissingMemberHandling.mjs +0 -5
- package/dist/enums/NullValueHandling.mjs +0 -5
- package/dist/enums/ReferenceLoopHandling.mjs +0 -6
- package/dist/index.mjs +0 -19
- package/dist/models/AcceptedType.mjs +0 -4
- package/dist/models/SerializationSettings.mjs +0 -18
- package/dist/naming-strategies/INamingStrategy.mjs +0 -1
- package/dist/naming-strategies/KebabCaseNamingStrategy.mjs +0 -11
- package/dist/naming-strategies/PascalCaseNamingStrategy.mjs +0 -8
- package/dist/naming-strategies/SnakeCaseNamingStrategy.mjs +0 -11
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AcceptedTypes } from "../models/AcceptedType";
|
|
2
|
-
import { SerializationSettings } from "../models/SerializationSettings";
|
|
1
|
+
import type { AcceptedTypes } from "../models/AcceptedType.js";
|
|
2
|
+
import { SerializationSettings } from "../models/SerializationSettings.js";
|
|
3
3
|
/**
|
|
4
4
|
* Class how help you deserialize object to classes.
|
|
5
5
|
*
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/* eslint-disable no-prototype-builtins */
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
4
3
|
/* eslint-disable complexity */
|
|
5
4
|
/* eslint-disable max-lines-per-function */
|
|
6
5
|
/* eslint-disable max-statements */
|
|
7
6
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
8
|
-
|
|
9
|
-
exports.Serializable = void 0;
|
|
10
|
-
const SerializationSettings_1 = require("../models/SerializationSettings");
|
|
7
|
+
import { SerializationSettings } from "../models/SerializationSettings.js";
|
|
11
8
|
/**
|
|
12
9
|
* Class how help you deserialize object to classes.
|
|
13
10
|
*
|
|
14
11
|
* @export
|
|
15
12
|
* @class Serializable
|
|
16
13
|
*/
|
|
17
|
-
class Serializable {
|
|
14
|
+
export class Serializable {
|
|
18
15
|
/**
|
|
19
16
|
* Deserialize object from static method.
|
|
20
17
|
*
|
|
@@ -52,10 +49,10 @@ class Serializable {
|
|
|
52
49
|
// Naming strategy and jsonName decorator
|
|
53
50
|
let jsonProp = this.getJsonPropertyName(thisProp, settings);
|
|
54
51
|
// For deep copy
|
|
55
|
-
if (!
|
|
52
|
+
if (!unknownJson?.hasOwnProperty(jsonProp) && unknownJson?.hasOwnProperty(thisProp)) {
|
|
56
53
|
jsonProp = thisProp;
|
|
57
54
|
}
|
|
58
|
-
if (
|
|
55
|
+
if (unknownJson?.hasOwnProperty(jsonProp) &&
|
|
59
56
|
this.hasOwnProperty(thisProp) &&
|
|
60
57
|
Reflect.hasMetadata("ts-serializable:jsonTypes", this.constructor.prototype, thisProp)) {
|
|
61
58
|
const acceptedTypes = Reflect.getMetadata("ts-serializable:jsonTypes", this.constructor.prototype, thisProp);
|
|
@@ -73,7 +70,7 @@ class Serializable {
|
|
|
73
70
|
* @memberof Serializable
|
|
74
71
|
*/
|
|
75
72
|
toJSON() {
|
|
76
|
-
const fromJson =
|
|
73
|
+
const fromJson = { ...this };
|
|
77
74
|
const toJson = {};
|
|
78
75
|
for (const prop in fromJson) {
|
|
79
76
|
// Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods
|
|
@@ -193,25 +190,23 @@ class Serializable {
|
|
|
193
190
|
return Reflect.get(this, prop);
|
|
194
191
|
}
|
|
195
192
|
getJsonPropertyName(thisProperty, settings) {
|
|
196
|
-
var _a, _b, _c;
|
|
197
193
|
if (Reflect.hasMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty)) {
|
|
198
194
|
return Reflect.getMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty);
|
|
199
195
|
}
|
|
200
|
-
if (settings
|
|
196
|
+
if (settings?.namingStrategy) {
|
|
201
197
|
return settings.namingStrategy.toJsonName(thisProperty);
|
|
202
198
|
}
|
|
203
199
|
if (Reflect.hasMetadata("ts-serializable:jsonObject", this.constructor)) {
|
|
204
200
|
const objectSettings = Reflect.getMetadata("ts-serializable:jsonObject", this.constructor);
|
|
205
|
-
return
|
|
201
|
+
return objectSettings.namingStrategy?.toJsonName(thisProperty) ?? thisProperty;
|
|
206
202
|
}
|
|
207
203
|
if (Serializable.defaultSettings.namingStrategy) {
|
|
208
204
|
const { namingStrategy } = Serializable.defaultSettings;
|
|
209
|
-
return
|
|
205
|
+
return namingStrategy.toJsonName(thisProperty) ?? thisProperty;
|
|
210
206
|
}
|
|
211
207
|
return thisProperty;
|
|
212
208
|
}
|
|
213
209
|
}
|
|
214
|
-
exports.Serializable = Serializable;
|
|
215
210
|
/**
|
|
216
211
|
* Global setting for serialization and deserialization
|
|
217
212
|
*
|
|
@@ -219,4 +214,4 @@ exports.Serializable = Serializable;
|
|
|
219
214
|
* @type {SerializationSettings}
|
|
220
215
|
* @memberof Serializable
|
|
221
216
|
*/
|
|
222
|
-
Serializable.defaultSettings = new
|
|
217
|
+
Serializable.defaultSettings = new SerializationSettings();
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jsonIgnore = void 0;
|
|
4
|
-
const jsonIgnore = () => (target, propertyKey) => {
|
|
1
|
+
export const jsonIgnore = () => (target, propertyKey) => {
|
|
5
2
|
Reflect.defineMetadata("ts-serializable:jsonIgnore", true, target, propertyKey);
|
|
6
3
|
};
|
|
7
|
-
exports.jsonIgnore = jsonIgnore;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jsonName = void 0;
|
|
4
|
-
const jsonName = (jsonPropertyName) => (target, propertyKey) => {
|
|
1
|
+
export const jsonName = (jsonPropertyName) => (target, propertyKey) => {
|
|
5
2
|
Reflect.defineMetadata("ts-serializable:jsonName", jsonPropertyName, target, propertyKey);
|
|
6
3
|
};
|
|
7
|
-
exports.jsonName = jsonName;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { SerializationSettings } from "../models/SerializationSettings";
|
|
1
|
+
import type { SerializationSettings } from "../models/SerializationSettings.js";
|
|
2
2
|
export declare const jsonObject: (settings?: Partial<SerializationSettings>, extend?: boolean) => ClassDecorator;
|
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.jsonObject = void 0;
|
|
4
|
-
const Serializable_1 = require("../classes/Serializable");
|
|
5
|
-
const jsonObject = (settings, extend) => (target) => {
|
|
1
|
+
import { Serializable } from "../classes/Serializable.js";
|
|
2
|
+
export const jsonObject = (settings, extend) => (target) => {
|
|
6
3
|
if (extend === true) {
|
|
7
|
-
Reflect.set(target, "defaultSettings",
|
|
8
|
-
Reflect.set(target, "fromJSON",
|
|
9
|
-
Reflect.set(target.prototype, "fromJSON",
|
|
10
|
-
Reflect.set(target.prototype, "deserializeProperty",
|
|
11
|
-
Reflect.set(target.prototype, "getJsonPropertyName",
|
|
12
|
-
Reflect.set(target.prototype, "onWrongType",
|
|
13
|
-
Reflect.set(target.prototype, "toJSON",
|
|
4
|
+
Reflect.set(target, "defaultSettings", Serializable.defaultSettings);
|
|
5
|
+
Reflect.set(target, "fromJSON", Serializable.fromJSON);
|
|
6
|
+
Reflect.set(target.prototype, "fromJSON", Serializable.prototype.fromJSON);
|
|
7
|
+
Reflect.set(target.prototype, "deserializeProperty", Serializable.prototype.deserializeProperty);
|
|
8
|
+
Reflect.set(target.prototype, "getJsonPropertyName", Serializable.prototype.getJsonPropertyName);
|
|
9
|
+
Reflect.set(target.prototype, "onWrongType", Serializable.prototype.onWrongType);
|
|
10
|
+
Reflect.set(target.prototype, "toJSON", Serializable.prototype.toJSON);
|
|
14
11
|
Reflect.defineMetadata("ts-serializable:jsonObjectExtended", true, target);
|
|
15
12
|
}
|
|
16
13
|
if (settings) {
|
|
17
14
|
Reflect.defineMetadata("ts-serializable:jsonObject", settings, target);
|
|
18
15
|
}
|
|
19
16
|
};
|
|
20
|
-
exports.jsonObject = jsonObject;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { AcceptedTypes } from "./../models/AcceptedType";
|
|
1
|
+
import type { AcceptedTypes } from "./../models/AcceptedType.js";
|
|
2
2
|
export declare const jsonProperty: (...args: AcceptedTypes[]) => PropertyDecorator;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jsonProperty = void 0;
|
|
4
|
-
const jsonProperty = (...args) => (target, propertyKey) => {
|
|
1
|
+
export const jsonProperty = (...args) => (target, propertyKey) => {
|
|
5
2
|
Reflect.defineMetadata("ts-serializable:jsonTypes", args, target, propertyKey);
|
|
6
3
|
};
|
|
7
|
-
exports.jsonProperty = jsonProperty;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DateFormatHandling = void 0;
|
|
4
|
-
var DateFormatHandling;
|
|
1
|
+
export var DateFormatHandling;
|
|
5
2
|
(function (DateFormatHandling) {
|
|
6
3
|
DateFormatHandling[DateFormatHandling["IsoDateFormat"] = 0] = "IsoDateFormat";
|
|
7
4
|
DateFormatHandling[DateFormatHandling["MicrosoftDateFormat"] = 1] = "MicrosoftDateFormat"; // Not supported yet
|
|
8
|
-
})(DateFormatHandling
|
|
5
|
+
})(DateFormatHandling || (DateFormatHandling = {}));
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultValueHandling = void 0;
|
|
4
|
-
var DefaultValueHandling;
|
|
1
|
+
export var DefaultValueHandling;
|
|
5
2
|
(function (DefaultValueHandling) {
|
|
6
3
|
DefaultValueHandling[DefaultValueHandling["Include"] = 0] = "Include";
|
|
7
4
|
DefaultValueHandling[DefaultValueHandling["Ignore"] = 1] = "Ignore";
|
|
8
5
|
DefaultValueHandling[DefaultValueHandling["Populate"] = 2] = "Populate";
|
|
9
6
|
DefaultValueHandling[DefaultValueHandling["IgnoreAndPopulate"] = 3] = "IgnoreAndPopulate"; // Not supported yet
|
|
10
|
-
})(DefaultValueHandling
|
|
7
|
+
})(DefaultValueHandling || (DefaultValueHandling = {}));
|
package/dist/enums/LogLevels.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LogLevels = void 0;
|
|
4
|
-
var LogLevels;
|
|
1
|
+
export var LogLevels;
|
|
5
2
|
(function (LogLevels) {
|
|
6
3
|
LogLevels[LogLevels["Trace"] = 0] = "Trace";
|
|
7
4
|
LogLevels[LogLevels["Debug"] = 1] = "Debug";
|
|
@@ -10,4 +7,4 @@ var LogLevels;
|
|
|
10
7
|
LogLevels[LogLevels["Error"] = 4] = "Error";
|
|
11
8
|
LogLevels[LogLevels["Critical"] = 5] = "Critical";
|
|
12
9
|
LogLevels[LogLevels["None"] = 6] = "None";
|
|
13
|
-
})(LogLevels
|
|
10
|
+
})(LogLevels || (LogLevels = {}));
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MissingMemberHandling = void 0;
|
|
4
|
-
var MissingMemberHandling;
|
|
1
|
+
export var MissingMemberHandling;
|
|
5
2
|
(function (MissingMemberHandling) {
|
|
6
3
|
MissingMemberHandling[MissingMemberHandling["Ignore"] = 0] = "Ignore";
|
|
7
4
|
MissingMemberHandling[MissingMemberHandling["Error"] = 1] = "Error"; // Not supported yet
|
|
8
|
-
})(MissingMemberHandling
|
|
5
|
+
})(MissingMemberHandling || (MissingMemberHandling = {}));
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NullValueHandling = void 0;
|
|
4
|
-
var NullValueHandling;
|
|
1
|
+
export var NullValueHandling;
|
|
5
2
|
(function (NullValueHandling) {
|
|
6
3
|
NullValueHandling[NullValueHandling["Include"] = 0] = "Include";
|
|
7
4
|
NullValueHandling[NullValueHandling["Ignore"] = 1] = "Ignore"; // Not supported yet
|
|
8
|
-
})(NullValueHandling
|
|
5
|
+
})(NullValueHandling || (NullValueHandling = {}));
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ReferenceLoopHandling = void 0;
|
|
4
|
-
var ReferenceLoopHandling;
|
|
1
|
+
export var ReferenceLoopHandling;
|
|
5
2
|
(function (ReferenceLoopHandling) {
|
|
6
3
|
ReferenceLoopHandling[ReferenceLoopHandling["Error"] = 0] = "Error";
|
|
7
4
|
ReferenceLoopHandling[ReferenceLoopHandling["Ignore"] = 1] = "Ignore";
|
|
8
5
|
ReferenceLoopHandling[ReferenceLoopHandling["Serialize"] = 2] = "Serialize";
|
|
9
|
-
})(ReferenceLoopHandling
|
|
6
|
+
})(ReferenceLoopHandling || (ReferenceLoopHandling = {}));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export { jsonIgnore } from "./decorators/JsonIgnore";
|
|
2
|
-
export { jsonName } from "./decorators/JsonName";
|
|
3
|
-
export { jsonObject } from "./decorators/JsonObject";
|
|
4
|
-
export { jsonProperty } from "./decorators/JsonProperty";
|
|
5
|
-
export { Serializable } from "./classes/Serializable";
|
|
6
|
-
export { DateFormatHandling } from "./enums/DateFormatHandling";
|
|
7
|
-
export { DefaultValueHandling } from "./enums/DefaultValueHandling";
|
|
8
|
-
export { MissingMemberHandling } from "./enums/MissingMemberHandling";
|
|
9
|
-
export { NullValueHandling } from "./enums/NullValueHandling";
|
|
10
|
-
export { ReferenceLoopHandling } from "./enums/ReferenceLoopHandling";
|
|
11
|
-
export { LogLevels } from "./enums/LogLevels";
|
|
12
|
-
export { SerializationSettings } from "./models/SerializationSettings";
|
|
13
|
-
export type { INamingStrategy } from "./naming-strategies/INamingStrategy";
|
|
14
|
-
export { SnakeCaseNamingStrategy } from "./naming-strategies/SnakeCaseNamingStrategy";
|
|
15
|
-
export { PascalCaseNamingStrategy } from "./naming-strategies/PascalCaseNamingStrategy";
|
|
16
|
-
export { KebabCaseNamingStrategy } from "./naming-strategies/KebabCaseNamingStrategy";
|
|
1
|
+
export { jsonIgnore } from "./decorators/JsonIgnore.js";
|
|
2
|
+
export { jsonName } from "./decorators/JsonName.js";
|
|
3
|
+
export { jsonObject } from "./decorators/JsonObject.js";
|
|
4
|
+
export { jsonProperty } from "./decorators/JsonProperty.js";
|
|
5
|
+
export { Serializable } from "./classes/Serializable.js";
|
|
6
|
+
export { DateFormatHandling } from "./enums/DateFormatHandling.js";
|
|
7
|
+
export { DefaultValueHandling } from "./enums/DefaultValueHandling.js";
|
|
8
|
+
export { MissingMemberHandling } from "./enums/MissingMemberHandling.js";
|
|
9
|
+
export { NullValueHandling } from "./enums/NullValueHandling.js";
|
|
10
|
+
export { ReferenceLoopHandling } from "./enums/ReferenceLoopHandling.js";
|
|
11
|
+
export { LogLevels } from "./enums/LogLevels.js";
|
|
12
|
+
export { SerializationSettings } from "./models/SerializationSettings.js";
|
|
13
|
+
export type { INamingStrategy } from "./naming-strategies/INamingStrategy.js";
|
|
14
|
+
export { SnakeCaseNamingStrategy } from "./naming-strategies/SnakeCaseNamingStrategy.js";
|
|
15
|
+
export { PascalCaseNamingStrategy } from "./naming-strategies/PascalCaseNamingStrategy.js";
|
|
16
|
+
export { KebabCaseNamingStrategy } from "./naming-strategies/KebabCaseNamingStrategy.js";
|
package/dist/index.js
CHANGED
|
@@ -1,37 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KebabCaseNamingStrategy = exports.PascalCaseNamingStrategy = exports.SnakeCaseNamingStrategy = exports.SerializationSettings = exports.LogLevels = exports.ReferenceLoopHandling = exports.NullValueHandling = exports.MissingMemberHandling = exports.DefaultValueHandling = exports.DateFormatHandling = exports.Serializable = exports.jsonProperty = exports.jsonObject = exports.jsonName = exports.jsonIgnore = void 0;
|
|
4
1
|
// Decoratore
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var JsonObject_1 = require("./decorators/JsonObject");
|
|
10
|
-
Object.defineProperty(exports, "jsonObject", { enumerable: true, get: function () { return JsonObject_1.jsonObject; } });
|
|
11
|
-
var JsonProperty_1 = require("./decorators/JsonProperty");
|
|
12
|
-
Object.defineProperty(exports, "jsonProperty", { enumerable: true, get: function () { return JsonProperty_1.jsonProperty; } });
|
|
2
|
+
export { jsonIgnore } from "./decorators/JsonIgnore.js";
|
|
3
|
+
export { jsonName } from "./decorators/JsonName.js";
|
|
4
|
+
export { jsonObject } from "./decorators/JsonObject.js";
|
|
5
|
+
export { jsonProperty } from "./decorators/JsonProperty.js";
|
|
13
6
|
// Base class
|
|
14
|
-
|
|
15
|
-
Object.defineProperty(exports, "Serializable", { enumerable: true, get: function () { return Serializable_1.Serializable; } });
|
|
7
|
+
export { Serializable } from "./classes/Serializable.js";
|
|
16
8
|
// Enums
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var NullValueHandling_1 = require("./enums/NullValueHandling");
|
|
24
|
-
Object.defineProperty(exports, "NullValueHandling", { enumerable: true, get: function () { return NullValueHandling_1.NullValueHandling; } });
|
|
25
|
-
var ReferenceLoopHandling_1 = require("./enums/ReferenceLoopHandling");
|
|
26
|
-
Object.defineProperty(exports, "ReferenceLoopHandling", { enumerable: true, get: function () { return ReferenceLoopHandling_1.ReferenceLoopHandling; } });
|
|
27
|
-
var LogLevels_1 = require("./enums/LogLevels");
|
|
28
|
-
Object.defineProperty(exports, "LogLevels", { enumerable: true, get: function () { return LogLevels_1.LogLevels; } });
|
|
9
|
+
export { DateFormatHandling } from "./enums/DateFormatHandling.js";
|
|
10
|
+
export { DefaultValueHandling } from "./enums/DefaultValueHandling.js";
|
|
11
|
+
export { MissingMemberHandling } from "./enums/MissingMemberHandling.js";
|
|
12
|
+
export { NullValueHandling } from "./enums/NullValueHandling.js";
|
|
13
|
+
export { ReferenceLoopHandling } from "./enums/ReferenceLoopHandling.js";
|
|
14
|
+
export { LogLevels } from "./enums/LogLevels.js";
|
|
29
15
|
// Settings
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
var PascalCaseNamingStrategy_1 = require("./naming-strategies/PascalCaseNamingStrategy");
|
|
35
|
-
Object.defineProperty(exports, "PascalCaseNamingStrategy", { enumerable: true, get: function () { return PascalCaseNamingStrategy_1.PascalCaseNamingStrategy; } });
|
|
36
|
-
var KebabCaseNamingStrategy_1 = require("./naming-strategies/KebabCaseNamingStrategy");
|
|
37
|
-
Object.defineProperty(exports, "KebabCaseNamingStrategy", { enumerable: true, get: function () { return KebabCaseNamingStrategy_1.KebabCaseNamingStrategy; } });
|
|
16
|
+
export { SerializationSettings } from "./models/SerializationSettings.js";
|
|
17
|
+
export { SnakeCaseNamingStrategy } from "./naming-strategies/SnakeCaseNamingStrategy.js";
|
|
18
|
+
export { PascalCaseNamingStrategy } from "./naming-strategies/PascalCaseNamingStrategy.js";
|
|
19
|
+
export { KebabCaseNamingStrategy } from "./naming-strategies/KebabCaseNamingStrategy.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type AcceptedType = BooleanConstructor | DateConstructor | NumberConstructor | ObjectConstructor | StringConstructor | SymbolConstructor | (new (...args: unknown[]) => object) | null | void;
|
|
2
2
|
interface IRecursiveArray<T> extends Array<IRecursiveArray<T> | T> {
|
|
3
3
|
}
|
|
4
|
-
export
|
|
4
|
+
export type AcceptedTypes = AcceptedType | IRecursiveArray<AcceptedType>;
|
|
5
5
|
export {};
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/sort-type-union-intersection-members */
|
|
3
1
|
/* eslint-disable @typescript-eslint/no-invalid-void-type */
|
|
4
2
|
/* eslint-disable @typescript-eslint/no-type-alias */
|
|
5
|
-
|
|
3
|
+
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { LogLevels } from "../enums/LogLevels";
|
|
2
|
-
import { DefaultValueHandling } from "../enums/DefaultValueHandling";
|
|
3
|
-
import { NullValueHandling } from "../enums/NullValueHandling";
|
|
4
|
-
import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling";
|
|
5
|
-
import { MissingMemberHandling } from "../enums/MissingMemberHandling";
|
|
6
|
-
import { DateFormatHandling } from "../enums/DateFormatHandling";
|
|
7
|
-
import type { INamingStrategy } from "../naming-strategies/INamingStrategy";
|
|
1
|
+
import { LogLevels } from "../enums/LogLevels.js";
|
|
2
|
+
import { DefaultValueHandling } from "../enums/DefaultValueHandling.js";
|
|
3
|
+
import { NullValueHandling } from "../enums/NullValueHandling.js";
|
|
4
|
+
import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling.js";
|
|
5
|
+
import { MissingMemberHandling } from "../enums/MissingMemberHandling.js";
|
|
6
|
+
import { DateFormatHandling } from "../enums/DateFormatHandling.js";
|
|
7
|
+
import type { INamingStrategy } from "../naming-strategies/INamingStrategy.js";
|
|
8
8
|
export declare class SerializationSettings {
|
|
9
9
|
dateFormatHandling: DateFormatHandling;
|
|
10
10
|
missingMemberHandling: MissingMemberHandling;
|
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const ReferenceLoopHandling_1 = require("../enums/ReferenceLoopHandling");
|
|
8
|
-
const MissingMemberHandling_1 = require("../enums/MissingMemberHandling");
|
|
9
|
-
const DateFormatHandling_1 = require("../enums/DateFormatHandling");
|
|
1
|
+
import { LogLevels } from "../enums/LogLevels.js";
|
|
2
|
+
import { DefaultValueHandling } from "../enums/DefaultValueHandling.js";
|
|
3
|
+
import { NullValueHandling } from "../enums/NullValueHandling.js";
|
|
4
|
+
import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling.js";
|
|
5
|
+
import { MissingMemberHandling } from "../enums/MissingMemberHandling.js";
|
|
6
|
+
import { DateFormatHandling } from "../enums/DateFormatHandling.js";
|
|
10
7
|
// From newtonsoft https://www.newtonsoft.com/json/help/html/SerializationSettings.htm
|
|
11
|
-
class SerializationSettings {
|
|
8
|
+
export class SerializationSettings {
|
|
12
9
|
constructor() {
|
|
13
|
-
this.dateFormatHandling =
|
|
14
|
-
this.missingMemberHandling =
|
|
15
|
-
this.referenceLoopHandling =
|
|
16
|
-
this.nullValueHandling =
|
|
17
|
-
this.defaultValueHandling =
|
|
10
|
+
this.dateFormatHandling = DateFormatHandling.IsoDateFormat;
|
|
11
|
+
this.missingMemberHandling = MissingMemberHandling.Ignore;
|
|
12
|
+
this.referenceLoopHandling = ReferenceLoopHandling.Serialize;
|
|
13
|
+
this.nullValueHandling = NullValueHandling.Include;
|
|
14
|
+
this.defaultValueHandling = DefaultValueHandling.Ignore;
|
|
18
15
|
this.namingStrategy = null;
|
|
19
|
-
this.logLevel =
|
|
16
|
+
this.logLevel = LogLevels.Warning;
|
|
20
17
|
}
|
|
21
18
|
}
|
|
22
|
-
exports.SerializationSettings = SerializationSettings;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KebabCaseNamingStrategy = void 0;
|
|
4
|
-
class KebabCaseNamingStrategy {
|
|
1
|
+
export class KebabCaseNamingStrategy {
|
|
5
2
|
fromJsonName(name) {
|
|
6
3
|
return name.replace(/-\w/gu, (group) => group[1].toUpperCase());
|
|
7
4
|
}
|
|
@@ -12,4 +9,3 @@ class KebabCaseNamingStrategy {
|
|
|
12
9
|
.toLowerCase();
|
|
13
10
|
}
|
|
14
11
|
}
|
|
15
|
-
exports.KebabCaseNamingStrategy = KebabCaseNamingStrategy;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PascalCaseNamingStrategy = void 0;
|
|
4
|
-
class PascalCaseNamingStrategy {
|
|
1
|
+
export class PascalCaseNamingStrategy {
|
|
5
2
|
fromJsonName(name) {
|
|
6
3
|
return name.slice(0, 1).toLowerCase() + name.slice(1, name.length);
|
|
7
4
|
}
|
|
@@ -9,4 +6,3 @@ class PascalCaseNamingStrategy {
|
|
|
9
6
|
return name.slice(0, 1).toUpperCase() + name.slice(1, name.length);
|
|
10
7
|
}
|
|
11
8
|
}
|
|
12
|
-
exports.PascalCaseNamingStrategy = PascalCaseNamingStrategy;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SnakeCaseNamingStrategy = void 0;
|
|
4
|
-
class SnakeCaseNamingStrategy {
|
|
1
|
+
export class SnakeCaseNamingStrategy {
|
|
5
2
|
fromJsonName(name) {
|
|
6
3
|
return name.replace(/_\w/gu, (group) => group[1].toUpperCase());
|
|
7
4
|
}
|
|
@@ -12,4 +9,3 @@ class SnakeCaseNamingStrategy {
|
|
|
12
9
|
.toLowerCase();
|
|
13
10
|
}
|
|
14
11
|
}
|
|
15
|
-
exports.SnakeCaseNamingStrategy = SnakeCaseNamingStrategy;
|
package/package.json
CHANGED
|
@@ -1,28 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-serializable",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Serialization and deserializtion for classes",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
|
-
"
|
|
6
|
+
"type": "module",
|
|
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": "^
|
|
13
|
-
"chai": "^4.3.
|
|
14
|
-
"mocha": "^10.
|
|
10
|
+
"@labeg/code-style": "^2.0.49",
|
|
11
|
+
"@types/chai": "^4.3.4",
|
|
12
|
+
"@types/mocha": "^10.0.0",
|
|
13
|
+
"chai": "^4.3.7",
|
|
14
|
+
"mocha": "^10.1.0",
|
|
15
15
|
"reflect-metadata": "^0.1.13",
|
|
16
|
-
"ts-node": "^10.
|
|
17
|
-
"typescript": "^4.
|
|
16
|
+
"ts-node": "^10.9.1",
|
|
17
|
+
"typescript": "^4.9.3"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
-
"cs:eslint": "eslint --fix -c .eslintrc.
|
|
20
|
+
"cs:eslint": "eslint --fix -c .eslintrc.cjs --ext .tsx,.ts,.jsx,.js ./src/ ./tests/",
|
|
21
21
|
"test": "mocha --reporter spec --require ts-node/register tests/*.test.ts",
|
|
22
|
-
"build
|
|
23
|
-
"build:commonjs": "tsc --project tsconfigbuild.es5.json",
|
|
24
|
-
"build:rename": "find ./dist/ -name \"*.js\" -exec rename .js .mjs {} +",
|
|
25
|
-
"build": "rm -rf dist/ && npm run build:esnext && npm run build:rename && npm run build:commonjs",
|
|
22
|
+
"build": "rm -rf dist/ && tsc --project tsconfigbuild.json && node ./dist/index.js",
|
|
26
23
|
"prepublishOnly": "npm run cs:eslint && npm run build && npm run test && npm version patch"
|
|
27
24
|
},
|
|
28
25
|
"repository": {
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
/* eslint-disable max-statements */
|
|
6
6
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
7
|
|
|
8
|
-
import type {AcceptedTypes} from "../models/AcceptedType";
|
|
9
|
-
import {SerializationSettings} from "../models/SerializationSettings";
|
|
8
|
+
import type {AcceptedTypes} from "../models/AcceptedType.js";
|
|
9
|
+
import {SerializationSettings} from "../models/SerializationSettings.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Class how help you deserialize object to classes.
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
4
4
|
/* eslint-disable @typescript-eslint/unbound-method */
|
|
5
5
|
/* eslint-disable max-statements */
|
|
6
|
-
import type {SerializationSettings} from "../models/SerializationSettings";
|
|
7
|
-
import {Serializable} from "../classes/Serializable";
|
|
6
|
+
import type {SerializationSettings} from "../models/SerializationSettings.js";
|
|
7
|
+
import {Serializable} from "../classes/Serializable.js";
|
|
8
8
|
|
|
9
9
|
export const jsonObject = (
|
|
10
10
|
settings?: Partial<SerializationSettings>,
|
package/src/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
// Decoratore
|
|
2
|
-
export {jsonIgnore} from "./decorators/JsonIgnore";
|
|
3
|
-
export {jsonName} from "./decorators/JsonName";
|
|
4
|
-
export {jsonObject} from "./decorators/JsonObject";
|
|
5
|
-
export {jsonProperty} from "./decorators/JsonProperty";
|
|
2
|
+
export {jsonIgnore} from "./decorators/JsonIgnore.js";
|
|
3
|
+
export {jsonName} from "./decorators/JsonName.js";
|
|
4
|
+
export {jsonObject} from "./decorators/JsonObject.js";
|
|
5
|
+
export {jsonProperty} from "./decorators/JsonProperty.js";
|
|
6
6
|
|
|
7
7
|
// Base class
|
|
8
|
-
export {Serializable} from "./classes/Serializable";
|
|
8
|
+
export {Serializable} from "./classes/Serializable.js";
|
|
9
9
|
|
|
10
10
|
// Enums
|
|
11
|
-
export {DateFormatHandling} from "./enums/DateFormatHandling";
|
|
12
|
-
export {DefaultValueHandling} from "./enums/DefaultValueHandling";
|
|
13
|
-
export {MissingMemberHandling} from "./enums/MissingMemberHandling";
|
|
14
|
-
export {NullValueHandling} from "./enums/NullValueHandling";
|
|
15
|
-
export {ReferenceLoopHandling} from "./enums/ReferenceLoopHandling";
|
|
16
|
-
export {LogLevels} from "./enums/LogLevels";
|
|
11
|
+
export {DateFormatHandling} from "./enums/DateFormatHandling.js";
|
|
12
|
+
export {DefaultValueHandling} from "./enums/DefaultValueHandling.js";
|
|
13
|
+
export {MissingMemberHandling} from "./enums/MissingMemberHandling.js";
|
|
14
|
+
export {NullValueHandling} from "./enums/NullValueHandling.js";
|
|
15
|
+
export {ReferenceLoopHandling} from "./enums/ReferenceLoopHandling.js";
|
|
16
|
+
export {LogLevels} from "./enums/LogLevels.js";
|
|
17
17
|
|
|
18
18
|
// Settings
|
|
19
|
-
export {SerializationSettings} from "./models/SerializationSettings";
|
|
19
|
+
export {SerializationSettings} from "./models/SerializationSettings.js";
|
|
20
20
|
|
|
21
21
|
// Naming strategies
|
|
22
|
-
export type {INamingStrategy} from "./naming-strategies/INamingStrategy";
|
|
23
|
-
export {SnakeCaseNamingStrategy} from "./naming-strategies/SnakeCaseNamingStrategy";
|
|
24
|
-
export {PascalCaseNamingStrategy} from "./naming-strategies/PascalCaseNamingStrategy";
|
|
25
|
-
export {KebabCaseNamingStrategy} from "./naming-strategies/KebabCaseNamingStrategy";
|
|
22
|
+
export type {INamingStrategy} from "./naming-strategies/INamingStrategy.js";
|
|
23
|
+
export {SnakeCaseNamingStrategy} from "./naming-strategies/SnakeCaseNamingStrategy.js";
|
|
24
|
+
export {PascalCaseNamingStrategy} from "./naming-strategies/PascalCaseNamingStrategy.js";
|
|
25
|
+
export {KebabCaseNamingStrategy} from "./naming-strategies/KebabCaseNamingStrategy.js";
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/sort-type-union-intersection-members */
|
|
2
1
|
/* eslint-disable @typescript-eslint/no-invalid-void-type */
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-type-alias */
|
|
4
3
|
|
|
5
|
-
export type AcceptedType =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
NumberConstructor |
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// Extended deserialization
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
export type AcceptedType =
|
|
5
|
+
BooleanConstructor |
|
|
6
|
+
DateConstructor |
|
|
7
|
+
NumberConstructor |
|
|
8
|
+
ObjectConstructor |
|
|
9
|
+
StringConstructor |
|
|
10
|
+
|
|
11
|
+
// Extended deserialization
|
|
12
|
+
SymbolConstructor |
|
|
13
|
+
(new (...args: unknown[]) => object) |
|
|
14
|
+
null |
|
|
15
|
+
void;
|
|
16
|
+
// Add ArrayBufferConstructor, MapConstructor, RegExpConstructor and many others...
|
|
15
17
|
|
|
16
18
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
17
19
|
interface IRecursiveArray<T> extends Array<IRecursiveArray<T> | T> {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
|
|
2
|
-
import {LogLevels} from "../enums/LogLevels";
|
|
3
|
-
import {DefaultValueHandling} from "../enums/DefaultValueHandling";
|
|
4
|
-
import {NullValueHandling} from "../enums/NullValueHandling";
|
|
5
|
-
import {ReferenceLoopHandling} from "../enums/ReferenceLoopHandling";
|
|
6
|
-
import {MissingMemberHandling} from "../enums/MissingMemberHandling";
|
|
7
|
-
import {DateFormatHandling} from "../enums/DateFormatHandling";
|
|
8
|
-
import type {INamingStrategy} from "../naming-strategies/INamingStrategy";
|
|
2
|
+
import {LogLevels} from "../enums/LogLevels.js";
|
|
3
|
+
import {DefaultValueHandling} from "../enums/DefaultValueHandling.js";
|
|
4
|
+
import {NullValueHandling} from "../enums/NullValueHandling.js";
|
|
5
|
+
import {ReferenceLoopHandling} from "../enums/ReferenceLoopHandling.js";
|
|
6
|
+
import {MissingMemberHandling} from "../enums/MissingMemberHandling.js";
|
|
7
|
+
import {DateFormatHandling} from "../enums/DateFormatHandling.js";
|
|
8
|
+
import type {INamingStrategy} from "../naming-strategies/INamingStrategy.js";
|
|
9
9
|
|
|
10
10
|
// From newtonsoft https://www.newtonsoft.com/json/help/html/SerializationSettings.htm
|
|
11
11
|
export class SerializationSettings {
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-prototype-builtins */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
3
|
-
/* eslint-disable complexity */
|
|
4
|
-
/* eslint-disable max-lines-per-function */
|
|
5
|
-
/* eslint-disable max-statements */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
|
-
import { SerializationSettings } from "../models/SerializationSettings";
|
|
8
|
-
/**
|
|
9
|
-
* Class how help you deserialize object to classes.
|
|
10
|
-
*
|
|
11
|
-
* @export
|
|
12
|
-
* @class Serializable
|
|
13
|
-
*/
|
|
14
|
-
export class Serializable {
|
|
15
|
-
/**
|
|
16
|
-
* Deserialize object from static method.
|
|
17
|
-
*
|
|
18
|
-
* Example:
|
|
19
|
-
* const obj: MyObject = MyObject.fromJSON({...data});
|
|
20
|
-
*
|
|
21
|
-
* @static
|
|
22
|
-
* @param {object} json
|
|
23
|
-
* @returns {object}
|
|
24
|
-
* @memberof Serializable
|
|
25
|
-
*/
|
|
26
|
-
static fromJSON(json, settings) {
|
|
27
|
-
return new this().fromJSON(json, settings);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Fill property of current model by data from json.
|
|
31
|
-
*
|
|
32
|
-
* Example:
|
|
33
|
-
* const obj: MyObject = new MyObject().fromJSON({...data});
|
|
34
|
-
*
|
|
35
|
-
* @param {object} ujson
|
|
36
|
-
* @returns {this}
|
|
37
|
-
* @memberof Serializable
|
|
38
|
-
*/
|
|
39
|
-
fromJSON(json, settings) {
|
|
40
|
-
const unknownJson = json;
|
|
41
|
-
if (unknownJson === null ||
|
|
42
|
-
Array.isArray(unknownJson) ||
|
|
43
|
-
typeof unknownJson !== "object") {
|
|
44
|
-
this.onWrongType(String(unknownJson), "is not object", unknownJson);
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
// eslint-disable-next-line guard-for-in
|
|
48
|
-
for (const thisProp in this) {
|
|
49
|
-
// Naming strategy and jsonName decorator
|
|
50
|
-
let jsonProp = this.getJsonPropertyName(thisProp, settings);
|
|
51
|
-
// For deep copy
|
|
52
|
-
if (!(unknownJson === null || unknownJson === void 0 ? void 0 : unknownJson.hasOwnProperty(jsonProp)) && (unknownJson === null || unknownJson === void 0 ? void 0 : unknownJson.hasOwnProperty(thisProp))) {
|
|
53
|
-
jsonProp = thisProp;
|
|
54
|
-
}
|
|
55
|
-
if ((unknownJson === null || unknownJson === void 0 ? void 0 : unknownJson.hasOwnProperty(jsonProp)) &&
|
|
56
|
-
this.hasOwnProperty(thisProp) &&
|
|
57
|
-
Reflect.hasMetadata("ts-serializable:jsonTypes", this.constructor.prototype, thisProp)) {
|
|
58
|
-
const acceptedTypes = Reflect.getMetadata("ts-serializable:jsonTypes", this.constructor.prototype, thisProp);
|
|
59
|
-
const jsonValue = Reflect.get(unknownJson, jsonProp);
|
|
60
|
-
const extractedValue = this.deserializeProperty(thisProp, acceptedTypes, jsonValue, settings);
|
|
61
|
-
Reflect.set(this, thisProp, extractedValue);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Process serialization for @jsonIgnore decorator
|
|
68
|
-
*
|
|
69
|
-
* @returns {object}
|
|
70
|
-
* @memberof Serializable
|
|
71
|
-
*/
|
|
72
|
-
toJSON() {
|
|
73
|
-
const fromJson = Object.assign({}, this);
|
|
74
|
-
const toJson = {};
|
|
75
|
-
for (const prop in fromJson) {
|
|
76
|
-
// Json.hasOwnProperty(prop) - preserve for deserialization for other classes with methods
|
|
77
|
-
if (fromJson.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
|
|
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));
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return toJson;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Process exceptions from wrong types.
|
|
88
|
-
* By default just print warning in console, but can by override for drop exception or logging to backend.
|
|
89
|
-
*
|
|
90
|
-
* @protected
|
|
91
|
-
* @param {string} prop
|
|
92
|
-
* @param {string} message
|
|
93
|
-
* @param {(unknown)} jsonValue
|
|
94
|
-
* @memberof Serializable
|
|
95
|
-
*/
|
|
96
|
-
onWrongType(prop, message, jsonValue) {
|
|
97
|
-
// eslint-disable-next-line no-console
|
|
98
|
-
console.error(`${this.constructor.name}.fromJSON: json.${prop} ${message}:`, jsonValue);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* //todo: write jsdoc
|
|
102
|
-
*
|
|
103
|
-
* @private
|
|
104
|
-
* @param {object} object
|
|
105
|
-
* @param {string} prop
|
|
106
|
-
* @param {AcceptedTypes[]} acceptedTypes
|
|
107
|
-
* @param {(unknown)} jsonValue
|
|
108
|
-
* @returns {(Object | null | void)}
|
|
109
|
-
* @memberof Serializable
|
|
110
|
-
*/
|
|
111
|
-
deserializeProperty(prop, acceptedTypes, jsonValue, settings) {
|
|
112
|
-
for (const acceptedType of acceptedTypes) { // Type Symbol is not a property
|
|
113
|
-
if ( // Null
|
|
114
|
-
acceptedType === null &&
|
|
115
|
-
jsonValue === null) {
|
|
116
|
-
return null;
|
|
117
|
-
}
|
|
118
|
-
else if ( // Void, for deep copy classes only, json don't have void type
|
|
119
|
-
acceptedType === void 0 &&
|
|
120
|
-
jsonValue === void 0) {
|
|
121
|
-
return void 0;
|
|
122
|
-
}
|
|
123
|
-
else if ( // Boolean, Boolean
|
|
124
|
-
acceptedType === Boolean &&
|
|
125
|
-
(typeof jsonValue === "boolean" || jsonValue instanceof Boolean)) {
|
|
126
|
-
return Boolean(jsonValue);
|
|
127
|
-
}
|
|
128
|
-
else if ( // Number, Number
|
|
129
|
-
acceptedType === Number &&
|
|
130
|
-
(typeof jsonValue === "number" || jsonValue instanceof Number)) {
|
|
131
|
-
return Number(jsonValue);
|
|
132
|
-
}
|
|
133
|
-
else if ( // String, String
|
|
134
|
-
acceptedType === String &&
|
|
135
|
-
(typeof jsonValue === "string" || jsonValue instanceof String)) {
|
|
136
|
-
return String(jsonValue);
|
|
137
|
-
}
|
|
138
|
-
else if ( // Object, Object
|
|
139
|
-
acceptedType === Object &&
|
|
140
|
-
(typeof jsonValue === "object")) {
|
|
141
|
-
return Object(jsonValue);
|
|
142
|
-
}
|
|
143
|
-
else if ( // Date
|
|
144
|
-
acceptedType === Date &&
|
|
145
|
-
(typeof jsonValue === "string" || jsonValue instanceof String || jsonValue instanceof Date)) {
|
|
146
|
-
// 0 year, 0 month, 0 days, 0 hours, 0 minutes, 0 seconds
|
|
147
|
-
let unicodeTime = new Date("0000-01-01T00:00:00.000").getTime();
|
|
148
|
-
if (typeof jsonValue === "string") {
|
|
149
|
-
unicodeTime = Date.parse(jsonValue);
|
|
150
|
-
}
|
|
151
|
-
else if (jsonValue instanceof String) {
|
|
152
|
-
unicodeTime = Date.parse(String(jsonValue));
|
|
153
|
-
}
|
|
154
|
-
else if (jsonValue instanceof Date) {
|
|
155
|
-
unicodeTime = jsonValue.getTime();
|
|
156
|
-
}
|
|
157
|
-
if (isNaN(unicodeTime)) { // Preserve invalid time
|
|
158
|
-
this.onWrongType(prop, "is invalid date", jsonValue);
|
|
159
|
-
}
|
|
160
|
-
return new Date(unicodeTime);
|
|
161
|
-
}
|
|
162
|
-
else if ( // Array
|
|
163
|
-
Array.isArray(acceptedType) &&
|
|
164
|
-
Array.isArray(jsonValue)) {
|
|
165
|
-
if (acceptedType[0] === void 0) {
|
|
166
|
-
this.onWrongType(prop, "invalid type", jsonValue);
|
|
167
|
-
}
|
|
168
|
-
return jsonValue.map((arrayValue) => this.deserializeProperty(prop, acceptedType, arrayValue, settings));
|
|
169
|
-
}
|
|
170
|
-
else if ( // Serializable
|
|
171
|
-
acceptedType !== null &&
|
|
172
|
-
acceptedType !== void 0 &&
|
|
173
|
-
!Array.isArray(acceptedType) &&
|
|
174
|
-
(acceptedType.prototype instanceof Serializable ||
|
|
175
|
-
Boolean(Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType))) &&
|
|
176
|
-
jsonValue !== null &&
|
|
177
|
-
jsonValue !== void 0 &&
|
|
178
|
-
typeof jsonValue === "object" && !Array.isArray(jsonValue)) {
|
|
179
|
-
const TypeConstructor = acceptedType;
|
|
180
|
-
return new TypeConstructor().fromJSON(jsonValue, settings);
|
|
181
|
-
}
|
|
182
|
-
else if ( // Instance any other class, not Serializable, for parse from other classes instance
|
|
183
|
-
acceptedType instanceof Function &&
|
|
184
|
-
jsonValue instanceof acceptedType) {
|
|
185
|
-
return jsonValue;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// Process wrong type and return default value
|
|
189
|
-
this.onWrongType(prop, "is invalid", jsonValue);
|
|
190
|
-
return Reflect.get(this, prop);
|
|
191
|
-
}
|
|
192
|
-
getJsonPropertyName(thisProperty, settings) {
|
|
193
|
-
var _a, _b, _c;
|
|
194
|
-
if (Reflect.hasMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty)) {
|
|
195
|
-
return Reflect.getMetadata("ts-serializable:jsonName", this.constructor.prototype, thisProperty);
|
|
196
|
-
}
|
|
197
|
-
if (settings === null || settings === void 0 ? void 0 : settings.namingStrategy) {
|
|
198
|
-
return settings.namingStrategy.toJsonName(thisProperty);
|
|
199
|
-
}
|
|
200
|
-
if (Reflect.hasMetadata("ts-serializable:jsonObject", this.constructor)) {
|
|
201
|
-
const objectSettings = Reflect.getMetadata("ts-serializable:jsonObject", this.constructor);
|
|
202
|
-
return (_b = (_a = objectSettings.namingStrategy) === null || _a === void 0 ? void 0 : _a.toJsonName(thisProperty)) !== null && _b !== void 0 ? _b : thisProperty;
|
|
203
|
-
}
|
|
204
|
-
if (Serializable.defaultSettings.namingStrategy) {
|
|
205
|
-
const { namingStrategy } = Serializable.defaultSettings;
|
|
206
|
-
return (_c = namingStrategy.toJsonName(thisProperty)) !== null && _c !== void 0 ? _c : thisProperty;
|
|
207
|
-
}
|
|
208
|
-
return thisProperty;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Global setting for serialization and deserialization
|
|
213
|
-
*
|
|
214
|
-
* @static
|
|
215
|
-
* @type {SerializationSettings}
|
|
216
|
-
* @memberof Serializable
|
|
217
|
-
*/
|
|
218
|
-
Serializable.defaultSettings = new SerializationSettings();
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Serializable } from "../classes/Serializable";
|
|
2
|
-
export const jsonObject = (settings, extend) => (target) => {
|
|
3
|
-
if (extend === true) {
|
|
4
|
-
Reflect.set(target, "defaultSettings", Serializable.defaultSettings);
|
|
5
|
-
Reflect.set(target, "fromJSON", Serializable.fromJSON);
|
|
6
|
-
Reflect.set(target.prototype, "fromJSON", Serializable.prototype.fromJSON);
|
|
7
|
-
Reflect.set(target.prototype, "deserializeProperty", Serializable.prototype.deserializeProperty);
|
|
8
|
-
Reflect.set(target.prototype, "getJsonPropertyName", Serializable.prototype.getJsonPropertyName);
|
|
9
|
-
Reflect.set(target.prototype, "onWrongType", Serializable.prototype.onWrongType);
|
|
10
|
-
Reflect.set(target.prototype, "toJSON", Serializable.prototype.toJSON);
|
|
11
|
-
Reflect.defineMetadata("ts-serializable:jsonObjectExtended", true, target);
|
|
12
|
-
}
|
|
13
|
-
if (settings) {
|
|
14
|
-
Reflect.defineMetadata("ts-serializable:jsonObject", settings, target);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export var DateFormatHandling;
|
|
2
|
-
(function (DateFormatHandling) {
|
|
3
|
-
DateFormatHandling[DateFormatHandling["IsoDateFormat"] = 0] = "IsoDateFormat";
|
|
4
|
-
DateFormatHandling[DateFormatHandling["MicrosoftDateFormat"] = 1] = "MicrosoftDateFormat"; // Not supported yet
|
|
5
|
-
})(DateFormatHandling || (DateFormatHandling = {}));
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export var DefaultValueHandling;
|
|
2
|
-
(function (DefaultValueHandling) {
|
|
3
|
-
DefaultValueHandling[DefaultValueHandling["Include"] = 0] = "Include";
|
|
4
|
-
DefaultValueHandling[DefaultValueHandling["Ignore"] = 1] = "Ignore";
|
|
5
|
-
DefaultValueHandling[DefaultValueHandling["Populate"] = 2] = "Populate";
|
|
6
|
-
DefaultValueHandling[DefaultValueHandling["IgnoreAndPopulate"] = 3] = "IgnoreAndPopulate"; // Not supported yet
|
|
7
|
-
})(DefaultValueHandling || (DefaultValueHandling = {}));
|
package/dist/enums/LogLevels.mjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export var LogLevels;
|
|
2
|
-
(function (LogLevels) {
|
|
3
|
-
LogLevels[LogLevels["Trace"] = 0] = "Trace";
|
|
4
|
-
LogLevels[LogLevels["Debug"] = 1] = "Debug";
|
|
5
|
-
LogLevels[LogLevels["Information"] = 2] = "Information";
|
|
6
|
-
LogLevels[LogLevels["Warning"] = 3] = "Warning";
|
|
7
|
-
LogLevels[LogLevels["Error"] = 4] = "Error";
|
|
8
|
-
LogLevels[LogLevels["Critical"] = 5] = "Critical";
|
|
9
|
-
LogLevels[LogLevels["None"] = 6] = "None";
|
|
10
|
-
})(LogLevels || (LogLevels = {}));
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export var MissingMemberHandling;
|
|
2
|
-
(function (MissingMemberHandling) {
|
|
3
|
-
MissingMemberHandling[MissingMemberHandling["Ignore"] = 0] = "Ignore";
|
|
4
|
-
MissingMemberHandling[MissingMemberHandling["Error"] = 1] = "Error"; // Not supported yet
|
|
5
|
-
})(MissingMemberHandling || (MissingMemberHandling = {}));
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export var ReferenceLoopHandling;
|
|
2
|
-
(function (ReferenceLoopHandling) {
|
|
3
|
-
ReferenceLoopHandling[ReferenceLoopHandling["Error"] = 0] = "Error";
|
|
4
|
-
ReferenceLoopHandling[ReferenceLoopHandling["Ignore"] = 1] = "Ignore";
|
|
5
|
-
ReferenceLoopHandling[ReferenceLoopHandling["Serialize"] = 2] = "Serialize";
|
|
6
|
-
})(ReferenceLoopHandling || (ReferenceLoopHandling = {}));
|
package/dist/index.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// Decoratore
|
|
2
|
-
export { jsonIgnore } from "./decorators/JsonIgnore";
|
|
3
|
-
export { jsonName } from "./decorators/JsonName";
|
|
4
|
-
export { jsonObject } from "./decorators/JsonObject";
|
|
5
|
-
export { jsonProperty } from "./decorators/JsonProperty";
|
|
6
|
-
// Base class
|
|
7
|
-
export { Serializable } from "./classes/Serializable";
|
|
8
|
-
// Enums
|
|
9
|
-
export { DateFormatHandling } from "./enums/DateFormatHandling";
|
|
10
|
-
export { DefaultValueHandling } from "./enums/DefaultValueHandling";
|
|
11
|
-
export { MissingMemberHandling } from "./enums/MissingMemberHandling";
|
|
12
|
-
export { NullValueHandling } from "./enums/NullValueHandling";
|
|
13
|
-
export { ReferenceLoopHandling } from "./enums/ReferenceLoopHandling";
|
|
14
|
-
export { LogLevels } from "./enums/LogLevels";
|
|
15
|
-
// Settings
|
|
16
|
-
export { SerializationSettings } from "./models/SerializationSettings";
|
|
17
|
-
export { SnakeCaseNamingStrategy } from "./naming-strategies/SnakeCaseNamingStrategy";
|
|
18
|
-
export { PascalCaseNamingStrategy } from "./naming-strategies/PascalCaseNamingStrategy";
|
|
19
|
-
export { KebabCaseNamingStrategy } from "./naming-strategies/KebabCaseNamingStrategy";
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { LogLevels } from "../enums/LogLevels";
|
|
2
|
-
import { DefaultValueHandling } from "../enums/DefaultValueHandling";
|
|
3
|
-
import { NullValueHandling } from "../enums/NullValueHandling";
|
|
4
|
-
import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling";
|
|
5
|
-
import { MissingMemberHandling } from "../enums/MissingMemberHandling";
|
|
6
|
-
import { DateFormatHandling } from "../enums/DateFormatHandling";
|
|
7
|
-
// From newtonsoft https://www.newtonsoft.com/json/help/html/SerializationSettings.htm
|
|
8
|
-
export class SerializationSettings {
|
|
9
|
-
constructor() {
|
|
10
|
-
this.dateFormatHandling = DateFormatHandling.IsoDateFormat;
|
|
11
|
-
this.missingMemberHandling = MissingMemberHandling.Ignore;
|
|
12
|
-
this.referenceLoopHandling = ReferenceLoopHandling.Serialize;
|
|
13
|
-
this.nullValueHandling = NullValueHandling.Include;
|
|
14
|
-
this.defaultValueHandling = DefaultValueHandling.Ignore;
|
|
15
|
-
this.namingStrategy = null;
|
|
16
|
-
this.logLevel = LogLevels.Warning;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|