ts-serializable 4.0.0 → 4.2.0
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/README.md +23 -23
- package/dist/classes/Serializable.d.ts +22 -22
- package/dist/classes/Serializable.js +27 -27
- package/dist/decorators/JsonIgnore.d.ts +3 -0
- package/dist/decorators/JsonIgnore.js +3 -0
- package/dist/decorators/JsonName.d.ts +5 -0
- package/dist/decorators/JsonName.js +5 -0
- package/dist/decorators/JsonObject.d.ts +5 -0
- package/dist/decorators/JsonObject.js +5 -0
- package/dist/decorators/JsonProperty.d.ts +5 -0
- package/dist/decorators/JsonProperty.js +5 -0
- package/dist/enums/DateFormatHandling.d.ts +3 -0
- package/dist/enums/DateFormatHandling.js +3 -0
- package/dist/enums/DefaultValueHandling.d.ts +3 -0
- package/dist/enums/DefaultValueHandling.js +3 -0
- package/dist/enums/LogLevels.d.ts +3 -0
- package/dist/enums/LogLevels.js +3 -0
- package/dist/enums/MissingMemberHandling.d.ts +3 -0
- package/dist/enums/MissingMemberHandling.js +3 -0
- package/dist/enums/NullValueHandling.d.ts +3 -0
- package/dist/enums/NullValueHandling.js +3 -0
- package/dist/enums/ReferenceLoopHandling.d.ts +3 -0
- package/dist/enums/ReferenceLoopHandling.js +3 -0
- package/dist/index.js +1 -1
- package/dist/models/AcceptedType.d.ts +6 -0
- package/dist/models/SerializationSettings.d.ts +24 -0
- package/dist/models/SerializationSettings.js +24 -0
- package/dist/naming-strategies/CamelCaseNamingStrategy.d.ts +15 -0
- package/dist/naming-strategies/CamelCaseNamingStrategy.js +15 -0
- package/dist/naming-strategies/INamingStrategy.d.ts +15 -0
- package/dist/naming-strategies/KebabCaseNamingStrategy.d.ts +15 -0
- package/dist/naming-strategies/KebabCaseNamingStrategy.js +15 -0
- package/dist/naming-strategies/PascalCaseNamingStrategy.d.ts +15 -0
- package/dist/naming-strategies/PascalCaseNamingStrategy.js +15 -0
- package/dist/utils/ClassToFormData.d.ts +8 -0
- package/dist/utils/ClassToFormData.js +10 -1
- package/dist/utils/GetProperyName.d.ts +8 -0
- package/dist/utils/GetProperyName.js +9 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
Serializable
|
|
2
2
|
=====
|
|
3
3
|
|
|
4
|
-
Small library for deserialization and serialization for
|
|
4
|
+
Small library for deserialization and serialization for JavaScript and TypeScript
|
|
5
5
|
|
|
6
6
|
Description
|
|
7
7
|
------
|
|
8
8
|
|
|
9
|
-
- For working this library
|
|
9
|
+
- For working, this library needs the Metadata Reflection API. If your platform (browser/Node.js) doesn't support it, you must use a polyfill. Example: [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)
|
|
10
10
|
|
|
11
|
-
- By default library
|
|
11
|
+
- By default, the library doesn't crash on wrong types in JSON and returns the default value on the wrong property. If you need more secure behavior, you must override the method `onWrongType` on the `Serializable` object and throw an exception in this method, according to your logic.
|
|
12
12
|
|
|
13
13
|
Installation
|
|
14
14
|
------
|
|
@@ -22,36 +22,36 @@ npm install ts-serializable
|
|
|
22
22
|
Usage
|
|
23
23
|
------
|
|
24
24
|
|
|
25
|
-
This example
|
|
25
|
+
This example is written in TypeScript, but if you remove typing, it will also work in JavaScript.
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
28
|
import { jsonProperty, Serializable } from "ts-serializable";
|
|
29
29
|
|
|
30
30
|
export class User extends Serializable {
|
|
31
31
|
|
|
32
|
-
// @jsonProperty
|
|
33
|
-
// properties
|
|
34
|
-
//
|
|
32
|
+
// @jsonProperty parameters are accepted types for JSON
|
|
33
|
+
// properties. If a property in JSON is not found or
|
|
34
|
+
// has an invalid type, it will return the default value.
|
|
35
35
|
@jsonProperty(Number, null)
|
|
36
|
-
public id: number | null = null; // default value
|
|
36
|
+
public id: number | null = null; // default value is necessary
|
|
37
37
|
|
|
38
38
|
@jsonProperty(String)
|
|
39
|
-
public firstName: string = ''; // default value
|
|
39
|
+
public firstName: string = ''; // default value is necessary
|
|
40
40
|
|
|
41
41
|
@jsonProperty(String)
|
|
42
|
-
public familyName: string = ''; // default value
|
|
42
|
+
public familyName: string = ''; // default value is necessary
|
|
43
43
|
|
|
44
44
|
@jsonProperty(String, void 0)
|
|
45
|
-
public lastName?: string = void 0; // default value
|
|
45
|
+
public lastName?: string = void 0; // default value is necessary
|
|
46
46
|
|
|
47
47
|
@jsonProperty(Date)
|
|
48
|
-
public birthdate: Date = new Date(); // default value
|
|
48
|
+
public birthdate: Date = new Date(); // default value is necessary
|
|
49
49
|
|
|
50
50
|
@jsonProperty([String])
|
|
51
|
-
public tags: string[] = []; // default value
|
|
51
|
+
public tags: string[] = []; // default value is necessary
|
|
52
52
|
|
|
53
53
|
@jsonProperty(OtherClassConstructor, null)
|
|
54
|
-
public other: OtherClassConstructor | null = null; // default value
|
|
54
|
+
public other: OtherClassConstructor | null = null; // default value is necessary
|
|
55
55
|
|
|
56
56
|
public getFullName(): string {
|
|
57
57
|
return [
|
|
@@ -79,19 +79,19 @@ user.getAge();
|
|
|
79
79
|
* With Serializable
|
|
80
80
|
*/
|
|
81
81
|
const user: User = new User().fromJSON(json);
|
|
82
|
-
user.getFullName(); //
|
|
83
|
-
user.getAge(); //
|
|
82
|
+
user.getFullName(); // works fine and returns a string
|
|
83
|
+
user.getAge(); // works fine and returns a number
|
|
84
84
|
|
|
85
85
|
// or
|
|
86
86
|
const user: User = User.fromJSON(json);
|
|
87
|
-
user.getFullName(); //
|
|
88
|
-
user.getAge(); //
|
|
87
|
+
user.getFullName(); // works fine and returns a string
|
|
88
|
+
user.getAge(); // works fine and returns a number
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
Naming strategies
|
|
92
92
|
------
|
|
93
93
|
|
|
94
|
-
Supported conversion between different naming cases, such as SnakeCase, KebabCase, PascalCase and CamelCase. Also you can set custom name for property of
|
|
94
|
+
Supported conversion between different naming cases, such as SnakeCase, KebabCase, PascalCase and CamelCase. Also, you can set a custom name for a property of a JSON object.
|
|
95
95
|
|
|
96
96
|
```typescript
|
|
97
97
|
const json = {
|
|
@@ -157,7 +157,7 @@ Supported settings:
|
|
|
157
157
|
View-Models from Backend Models
|
|
158
158
|
------
|
|
159
159
|
|
|
160
|
-
If you need to create view-model from
|
|
160
|
+
If you need to create a view-model from a DTO or entities model, you can use the same model. Just add a VM property to the DTO or entities model and mark this property with the @jsonIgnore() decorator, and this property will not be serialized to JSON.
|
|
161
161
|
|
|
162
162
|
```typescript
|
|
163
163
|
import { jsonProperty, jsonIgnore, Serializable } from "ts-serializable";
|
|
@@ -165,10 +165,10 @@ import { jsonProperty, jsonIgnore, Serializable } from "ts-serializable";
|
|
|
165
165
|
export class User extends Serializable {
|
|
166
166
|
|
|
167
167
|
@jsonProperty(String)
|
|
168
|
-
public firstName: string = ''; // default value
|
|
168
|
+
public firstName: string = ''; // default value is necessary
|
|
169
169
|
|
|
170
170
|
@jsonProperty(String)
|
|
171
|
-
public familyName: string = ''; // default value
|
|
171
|
+
public familyName: string = ''; // default value is necessary
|
|
172
172
|
|
|
173
173
|
@jsonIgnore()
|
|
174
174
|
public isExpanded: boolean = false;
|
|
@@ -184,7 +184,7 @@ JSON.stringify(user);
|
|
|
184
184
|
Class to FormData
|
|
185
185
|
------
|
|
186
186
|
|
|
187
|
-
Sometimes classes contain properties with the File type. Sending such classes via
|
|
187
|
+
Sometimes classes contain properties with the File type. Sending such classes via JSON is a heavy task. Converting a file property to JSON can freeze the interface for a few seconds if the file is large. A much better solution is to send an Ajax form. Example:
|
|
188
188
|
|
|
189
189
|
```typescript
|
|
190
190
|
import { Serializable } from "ts-serializable";
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { AcceptedTypes } from "../models/AcceptedType.js";
|
|
2
2
|
import { SerializationSettings } from "../models/SerializationSettings.js";
|
|
3
3
|
/**
|
|
4
|
-
* Class
|
|
4
|
+
* Class that helps you deserialize objects to classes.
|
|
5
5
|
*
|
|
6
6
|
* @export
|
|
7
7
|
* @class Serializable
|
|
8
8
|
*/
|
|
9
9
|
export declare class Serializable {
|
|
10
10
|
/**
|
|
11
|
-
* Global
|
|
11
|
+
* Global settings for serialization and deserialization.
|
|
12
12
|
*
|
|
13
13
|
* @static
|
|
14
14
|
* @type {SerializationSettings}
|
|
@@ -16,7 +16,7 @@ export declare class Serializable {
|
|
|
16
16
|
*/
|
|
17
17
|
static defaultSettings: SerializationSettings;
|
|
18
18
|
/**
|
|
19
|
-
* Deserialize object
|
|
19
|
+
* Deserialize an object using a static method.
|
|
20
20
|
*
|
|
21
21
|
* Example:
|
|
22
22
|
* const obj: MyObject = MyObject.fromJSON({...data});
|
|
@@ -28,22 +28,22 @@ export declare class Serializable {
|
|
|
28
28
|
*/
|
|
29
29
|
static fromJSON<T extends Serializable>(this: new () => T, json: object, settings?: Partial<SerializationSettings>): T;
|
|
30
30
|
/**
|
|
31
|
-
* Deserialize object from static method.
|
|
31
|
+
* Deserialize an object from a string using a static method.
|
|
32
32
|
*
|
|
33
33
|
* Example:
|
|
34
|
-
* const obj: MyObject = MyObject.fromString({...data});
|
|
34
|
+
* const obj: MyObject = MyObject.fromString("{...data}");
|
|
35
35
|
*
|
|
36
36
|
* @static
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {string} str
|
|
38
38
|
* @returns {object}
|
|
39
39
|
* @memberof Serializable
|
|
40
40
|
*/
|
|
41
41
|
static fromString<T extends Serializable>(this: new () => T, str: string, settings?: Partial<SerializationSettings>): T;
|
|
42
42
|
/**
|
|
43
|
-
* Fill
|
|
43
|
+
* Fill properties of the current model with data from a string.
|
|
44
44
|
*
|
|
45
45
|
* Example:
|
|
46
|
-
* const obj: MyObject = new MyObject().fromString("{...data}"
|
|
46
|
+
* const obj: MyObject = new MyObject().fromString("{...data}");
|
|
47
47
|
*
|
|
48
48
|
* @param {string} str
|
|
49
49
|
* @returns {this}
|
|
@@ -51,7 +51,7 @@ export declare class Serializable {
|
|
|
51
51
|
*/
|
|
52
52
|
fromString(str: string, settings?: Partial<SerializationSettings>): this;
|
|
53
53
|
/**
|
|
54
|
-
* Fill
|
|
54
|
+
* Fill properties of the current model with data from JSON.
|
|
55
55
|
*
|
|
56
56
|
* Example:
|
|
57
57
|
* const obj: MyObject = new MyObject().fromJSON({...data});
|
|
@@ -62,36 +62,36 @@ export declare class Serializable {
|
|
|
62
62
|
*/
|
|
63
63
|
fromJSON(json: object, settings?: Partial<SerializationSettings>): this;
|
|
64
64
|
/**
|
|
65
|
-
* Process serialization for @jsonIgnore decorator
|
|
65
|
+
* Process serialization for the @jsonIgnore decorator.
|
|
66
66
|
*
|
|
67
67
|
* @returns {object}
|
|
68
68
|
* @memberof Serializable
|
|
69
69
|
*/
|
|
70
70
|
toJSON(): Record<string, unknown>;
|
|
71
71
|
/**
|
|
72
|
-
* Serialize class to FormData.
|
|
72
|
+
* Serialize the class to FormData.
|
|
73
73
|
*
|
|
74
|
-
* Can be used
|
|
75
|
-
*
|
|
76
|
-
* user interface can
|
|
77
|
-
*
|
|
74
|
+
* Can be used to prepare an AJAX form with files.
|
|
75
|
+
* Sending files via AJAX JSON is a heavy task because it requires converting files to base64 format.
|
|
76
|
+
* The user interface can freeze for several seconds during this operation if the file is too large.
|
|
77
|
+
* AJAX forms are a lightweight alternative.
|
|
78
78
|
*
|
|
79
79
|
* @param {string} formPrefix Prefix for form property names
|
|
80
|
-
* @param {FormData} formData Can
|
|
80
|
+
* @param {FormData} formData Can update an existing FormData
|
|
81
81
|
* @returns {FormData}
|
|
82
82
|
* @memberof Serializable
|
|
83
83
|
*/
|
|
84
84
|
toFormData(formPrefix?: string, formData?: FormData): FormData;
|
|
85
85
|
/**
|
|
86
|
-
* Process serialization for @jsonIgnore decorator
|
|
86
|
+
* Process serialization for the @jsonIgnore decorator.
|
|
87
87
|
*
|
|
88
88
|
* @returns {string}
|
|
89
89
|
* @memberof Serializable
|
|
90
90
|
*/
|
|
91
91
|
toString(): string;
|
|
92
92
|
/**
|
|
93
|
-
* Process exceptions
|
|
94
|
-
* By default just
|
|
93
|
+
* Process exceptions for incorrect types.
|
|
94
|
+
* By default, it just prints a warning in the console, but it can be overridden to throw exceptions or log to the backend.
|
|
95
95
|
*
|
|
96
96
|
* @protected
|
|
97
97
|
* @param {string} prop
|
|
@@ -101,7 +101,7 @@ export declare class Serializable {
|
|
|
101
101
|
*/
|
|
102
102
|
protected onWrongType(prop: string, message: string, jsonValue: unknown): void;
|
|
103
103
|
/**
|
|
104
|
-
* Deserialize one property
|
|
104
|
+
* Deserialize one property.
|
|
105
105
|
*
|
|
106
106
|
* @private
|
|
107
107
|
* @param {object} object
|
|
@@ -113,10 +113,10 @@ export declare class Serializable {
|
|
|
113
113
|
*/
|
|
114
114
|
protected deserializeProperty(prop: string, acceptedTypes: AcceptedTypes[], jsonValue: unknown, settings?: Partial<SerializationSettings>): unknown;
|
|
115
115
|
/**
|
|
116
|
-
* Extract correct name for property.
|
|
116
|
+
* Extract the correct name for a property.
|
|
117
117
|
* Considers decorators for transforming the property name.
|
|
118
118
|
*
|
|
119
|
-
* @param {string} property Source name of property
|
|
119
|
+
* @param {string} property Source name of the property
|
|
120
120
|
* @param {Partial<SerializationSettings>} settings Serialization settings
|
|
121
121
|
* @returns
|
|
122
122
|
*/
|
|
@@ -9,14 +9,14 @@ import { SerializationSettings } from "../models/SerializationSettings.js";
|
|
|
9
9
|
import { classToFormData } from "../utils/ClassToFormData.js";
|
|
10
10
|
import { getPropertyName } from "../utils/GetProperyName.js";
|
|
11
11
|
/**
|
|
12
|
-
* Class
|
|
12
|
+
* Class that helps you deserialize objects to classes.
|
|
13
13
|
*
|
|
14
14
|
* @export
|
|
15
15
|
* @class Serializable
|
|
16
16
|
*/
|
|
17
17
|
export class Serializable {
|
|
18
18
|
/**
|
|
19
|
-
* Global
|
|
19
|
+
* Global settings for serialization and deserialization.
|
|
20
20
|
*
|
|
21
21
|
* @static
|
|
22
22
|
* @type {SerializationSettings}
|
|
@@ -24,7 +24,7 @@ export class Serializable {
|
|
|
24
24
|
*/
|
|
25
25
|
static defaultSettings = new SerializationSettings();
|
|
26
26
|
/**
|
|
27
|
-
* Deserialize object
|
|
27
|
+
* Deserialize an object using a static method.
|
|
28
28
|
*
|
|
29
29
|
* Example:
|
|
30
30
|
* const obj: MyObject = MyObject.fromJSON({...data});
|
|
@@ -38,13 +38,13 @@ export class Serializable {
|
|
|
38
38
|
return new this().fromJSON(json, settings);
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* Deserialize object from static method.
|
|
41
|
+
* Deserialize an object from a string using a static method.
|
|
42
42
|
*
|
|
43
43
|
* Example:
|
|
44
|
-
* const obj: MyObject = MyObject.fromString({...data});
|
|
44
|
+
* const obj: MyObject = MyObject.fromString("{...data}");
|
|
45
45
|
*
|
|
46
46
|
* @static
|
|
47
|
-
* @param {
|
|
47
|
+
* @param {string} str
|
|
48
48
|
* @returns {object}
|
|
49
49
|
* @memberof Serializable
|
|
50
50
|
*/
|
|
@@ -52,10 +52,10 @@ export class Serializable {
|
|
|
52
52
|
return new this().fromJSON(JSON.parse(str), settings);
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
|
-
* Fill
|
|
55
|
+
* Fill properties of the current model with data from a string.
|
|
56
56
|
*
|
|
57
57
|
* Example:
|
|
58
|
-
* const obj: MyObject = new MyObject().fromString("{...data}"
|
|
58
|
+
* const obj: MyObject = new MyObject().fromString("{...data}");
|
|
59
59
|
*
|
|
60
60
|
* @param {string} str
|
|
61
61
|
* @returns {this}
|
|
@@ -65,7 +65,7 @@ export class Serializable {
|
|
|
65
65
|
return this.fromJSON(JSON.parse(str), settings);
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* Fill
|
|
68
|
+
* Fill properties of the current model with data from JSON.
|
|
69
69
|
*
|
|
70
70
|
* Example:
|
|
71
71
|
* const obj: MyObject = new MyObject().fromJSON({...data});
|
|
@@ -79,7 +79,7 @@ export class Serializable {
|
|
|
79
79
|
if (unknownJson === null ||
|
|
80
80
|
Array.isArray(unknownJson) ||
|
|
81
81
|
typeof unknownJson !== "object") {
|
|
82
|
-
this.onWrongType(String(unknownJson), "is not object", unknownJson);
|
|
82
|
+
this.onWrongType(String(unknownJson), "is not an object", unknownJson);
|
|
83
83
|
return this;
|
|
84
84
|
}
|
|
85
85
|
// eslint-disable-next-line guard-for-in
|
|
@@ -102,7 +102,7 @@ export class Serializable {
|
|
|
102
102
|
return this;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
* Process serialization for @jsonIgnore decorator
|
|
105
|
+
* Process serialization for the @jsonIgnore decorator.
|
|
106
106
|
*
|
|
107
107
|
* @returns {object}
|
|
108
108
|
* @memberof Serializable
|
|
@@ -125,15 +125,15 @@ export class Serializable {
|
|
|
125
125
|
return toJson;
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
128
|
-
* Serialize class to FormData.
|
|
128
|
+
* Serialize the class to FormData.
|
|
129
129
|
*
|
|
130
|
-
* Can be used
|
|
131
|
-
*
|
|
132
|
-
* user interface can
|
|
133
|
-
*
|
|
130
|
+
* Can be used to prepare an AJAX form with files.
|
|
131
|
+
* Sending files via AJAX JSON is a heavy task because it requires converting files to base64 format.
|
|
132
|
+
* The user interface can freeze for several seconds during this operation if the file is too large.
|
|
133
|
+
* AJAX forms are a lightweight alternative.
|
|
134
134
|
*
|
|
135
135
|
* @param {string} formPrefix Prefix for form property names
|
|
136
|
-
* @param {FormData} formData Can
|
|
136
|
+
* @param {FormData} formData Can update an existing FormData
|
|
137
137
|
* @returns {FormData}
|
|
138
138
|
* @memberof Serializable
|
|
139
139
|
*/
|
|
@@ -141,7 +141,7 @@ export class Serializable {
|
|
|
141
141
|
return classToFormData(this, formPrefix, formData);
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
|
-
* Process serialization for @jsonIgnore decorator
|
|
144
|
+
* Process serialization for the @jsonIgnore decorator.
|
|
145
145
|
*
|
|
146
146
|
* @returns {string}
|
|
147
147
|
* @memberof Serializable
|
|
@@ -150,8 +150,8 @@ export class Serializable {
|
|
|
150
150
|
return JSON.stringify(this.toJSON());
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
153
|
-
* Process exceptions
|
|
154
|
-
* By default just
|
|
153
|
+
* Process exceptions for incorrect types.
|
|
154
|
+
* By default, it just prints a warning in the console, but it can be overridden to throw exceptions or log to the backend.
|
|
155
155
|
*
|
|
156
156
|
* @protected
|
|
157
157
|
* @param {string} prop
|
|
@@ -164,7 +164,7 @@ export class Serializable {
|
|
|
164
164
|
console.error(`${this.constructor.name}.fromJSON: json.${prop} ${message}:`, jsonValue);
|
|
165
165
|
}
|
|
166
166
|
/**
|
|
167
|
-
* Deserialize one property
|
|
167
|
+
* Deserialize one property.
|
|
168
168
|
*
|
|
169
169
|
* @private
|
|
170
170
|
* @param {object} object
|
|
@@ -182,7 +182,7 @@ export class Serializable {
|
|
|
182
182
|
jsonValue === null) {
|
|
183
183
|
return null;
|
|
184
184
|
}
|
|
185
|
-
else if ( // Void, for deep copy classes only,
|
|
185
|
+
else if ( // Void, for deep copy classes only, JSON doesn't have a void type
|
|
186
186
|
acceptedType === void 0 &&
|
|
187
187
|
jsonValue === void 0) {
|
|
188
188
|
return void 0;
|
|
@@ -222,7 +222,7 @@ export class Serializable {
|
|
|
222
222
|
unicodeTime = jsonValue.getTime();
|
|
223
223
|
}
|
|
224
224
|
if (isNaN(unicodeTime)) { // Preserve invalid time
|
|
225
|
-
this.onWrongType(prop, "is invalid date", jsonValue);
|
|
225
|
+
this.onWrongType(prop, "is an invalid date", jsonValue);
|
|
226
226
|
}
|
|
227
227
|
return new Date(unicodeTime);
|
|
228
228
|
}
|
|
@@ -246,21 +246,21 @@ export class Serializable {
|
|
|
246
246
|
const TypeConstructor = acceptedType;
|
|
247
247
|
return new TypeConstructor().fromJSON(jsonValue, settings);
|
|
248
248
|
}
|
|
249
|
-
else if ( // Instance any other class, not Serializable, for
|
|
249
|
+
else if ( // Instance any other class, not Serializable, for parsing from other class instances
|
|
250
250
|
acceptedType instanceof Function &&
|
|
251
251
|
jsonValue instanceof acceptedType) {
|
|
252
252
|
return jsonValue;
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
|
-
// Process
|
|
255
|
+
// Process incorrect type and return default value
|
|
256
256
|
this.onWrongType(prop, "is invalid", jsonValue);
|
|
257
257
|
return Reflect.get(this, prop);
|
|
258
258
|
}
|
|
259
259
|
/**
|
|
260
|
-
* Extract correct name for property.
|
|
260
|
+
* Extract the correct name for a property.
|
|
261
261
|
* Considers decorators for transforming the property name.
|
|
262
262
|
*
|
|
263
|
-
* @param {string} property Source name of property
|
|
263
|
+
* @param {string} property Source name of the property
|
|
264
264
|
* @param {Partial<SerializationSettings>} settings Serialization settings
|
|
265
265
|
* @returns
|
|
266
266
|
*/
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator to specify a custom JSON property name for a class property.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} jsonPropertyName - The custom JSON property name.
|
|
5
|
+
*/
|
|
1
6
|
export const jsonName = (jsonPropertyName) => (target, propertyKey) => {
|
|
2
7
|
Reflect.defineMetadata("ts-serializable:jsonName", jsonPropertyName, target, propertyKey);
|
|
3
8
|
};
|
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import type { SerializationSettings } from "../models/SerializationSettings.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator to mark a class as serializable to JSON.
|
|
4
|
+
*
|
|
5
|
+
* @param {Partial<SerializationSettings>} [settings] - Optional serialization settings.
|
|
6
|
+
*/
|
|
2
7
|
export declare const jsonObject: (settings?: Partial<SerializationSettings>) => ClassDecorator;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator to mark a class as serializable to JSON.
|
|
3
|
+
*
|
|
4
|
+
* @param {Partial<SerializationSettings>} [settings] - Optional serialization settings.
|
|
5
|
+
*/
|
|
1
6
|
export const jsonObject = (settings) => (target) => {
|
|
2
7
|
if (settings) {
|
|
3
8
|
Reflect.defineMetadata("ts-serializable:jsonObject", settings, target);
|
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import type { AcceptedTypes } from "./../models/AcceptedType.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator to specify the accepted types for a JSON property.
|
|
4
|
+
*
|
|
5
|
+
* @param {...AcceptedTypes[]} args - The accepted types for the property.
|
|
6
|
+
*/
|
|
2
7
|
export declare const jsonProperty: (...args: AcceptedTypes[]) => PropertyDecorator;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator to specify the accepted types for a JSON property.
|
|
3
|
+
*
|
|
4
|
+
* @param {...AcceptedTypes[]} args - The accepted types for the property.
|
|
5
|
+
*/
|
|
1
6
|
export const jsonProperty = (...args) => (target, propertyKey) => {
|
|
2
7
|
Reflect.defineMetadata("ts-serializable:jsonTypes", args, target, propertyKey);
|
|
3
8
|
};
|
package/dist/enums/LogLevels.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definition for accepted types in serialization and deserialization.
|
|
3
|
+
*/
|
|
1
4
|
export type AcceptedType = BooleanConstructor | DateConstructor | NumberConstructor | ObjectConstructor | StringConstructor | SymbolConstructor | (new (...args: unknown[]) => object) | null | void;
|
|
2
5
|
type IRecursiveArray<T> = (IRecursiveArray<T> | T)[];
|
|
6
|
+
/**
|
|
7
|
+
* Type definition for recursive arrays of accepted types.
|
|
8
|
+
*/
|
|
3
9
|
export type AcceptedTypes = AcceptedType | IRecursiveArray<AcceptedType>;
|
|
4
10
|
export {};
|
|
@@ -5,12 +5,36 @@ import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling.js";
|
|
|
5
5
|
import { MissingMemberHandling } from "../enums/MissingMemberHandling.js";
|
|
6
6
|
import { DateFormatHandling } from "../enums/DateFormatHandling.js";
|
|
7
7
|
import type { INamingStrategy } from "../naming-strategies/INamingStrategy.js";
|
|
8
|
+
/**
|
|
9
|
+
* Class representing serialization settings.
|
|
10
|
+
*/
|
|
8
11
|
export declare class SerializationSettings {
|
|
12
|
+
/**
|
|
13
|
+
* Specifies how dates are formatted during serialization.
|
|
14
|
+
*/
|
|
9
15
|
dateFormatHandling: DateFormatHandling;
|
|
16
|
+
/**
|
|
17
|
+
* Specifies how missing members are handled during deserialization.
|
|
18
|
+
*/
|
|
10
19
|
missingMemberHandling: MissingMemberHandling;
|
|
20
|
+
/**
|
|
21
|
+
* Specifies how reference loops are handled during serialization.
|
|
22
|
+
*/
|
|
11
23
|
referenceLoopHandling: ReferenceLoopHandling;
|
|
24
|
+
/**
|
|
25
|
+
* Specifies how null values are handled during serialization.
|
|
26
|
+
*/
|
|
12
27
|
nullValueHandling: NullValueHandling;
|
|
28
|
+
/**
|
|
29
|
+
* Specifies how default values are handled during serialization.
|
|
30
|
+
*/
|
|
13
31
|
defaultValueHandling: DefaultValueHandling;
|
|
32
|
+
/**
|
|
33
|
+
* Specifies the naming strategy for property names during serialization.
|
|
34
|
+
*/
|
|
14
35
|
namingStrategy: INamingStrategy | null;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies the log level for serialization operations.
|
|
38
|
+
*/
|
|
15
39
|
logLevel: LogLevels;
|
|
16
40
|
}
|
|
@@ -5,12 +5,36 @@ import { ReferenceLoopHandling } from "../enums/ReferenceLoopHandling.js";
|
|
|
5
5
|
import { MissingMemberHandling } from "../enums/MissingMemberHandling.js";
|
|
6
6
|
import { DateFormatHandling } from "../enums/DateFormatHandling.js";
|
|
7
7
|
// From newtonsoft https://www.newtonsoft.com/json/help/html/SerializationSettings.htm
|
|
8
|
+
/**
|
|
9
|
+
* Class representing serialization settings.
|
|
10
|
+
*/
|
|
8
11
|
export class SerializationSettings {
|
|
12
|
+
/**
|
|
13
|
+
* Specifies how dates are formatted during serialization.
|
|
14
|
+
*/
|
|
9
15
|
dateFormatHandling = DateFormatHandling.IsoDateFormat;
|
|
16
|
+
/**
|
|
17
|
+
* Specifies how missing members are handled during deserialization.
|
|
18
|
+
*/
|
|
10
19
|
missingMemberHandling = MissingMemberHandling.Ignore;
|
|
20
|
+
/**
|
|
21
|
+
* Specifies how reference loops are handled during serialization.
|
|
22
|
+
*/
|
|
11
23
|
referenceLoopHandling = ReferenceLoopHandling.Serialize;
|
|
24
|
+
/**
|
|
25
|
+
* Specifies how null values are handled during serialization.
|
|
26
|
+
*/
|
|
12
27
|
nullValueHandling = NullValueHandling.Include;
|
|
28
|
+
/**
|
|
29
|
+
* Specifies how default values are handled during serialization.
|
|
30
|
+
*/
|
|
13
31
|
defaultValueHandling = DefaultValueHandling.Ignore;
|
|
32
|
+
/**
|
|
33
|
+
* Specifies the naming strategy for property names during serialization.
|
|
34
|
+
*/
|
|
14
35
|
namingStrategy = null;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies the log level for serialization operations.
|
|
38
|
+
*/
|
|
15
39
|
logLevel = LogLevels.Warning;
|
|
16
40
|
}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import type { INamingStrategy } from "./INamingStrategy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to camel case.
|
|
4
|
+
*/
|
|
2
5
|
export declare class CamelCaseNamingStrategy implements INamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a camel case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The camel case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a camel case property name to a JSON property name.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} name - The camel case property name.
|
|
17
|
+
* @returns {string} - The JSON property name.
|
|
18
|
+
*/
|
|
4
19
|
toJsonName(name: string): string;
|
|
5
20
|
}
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to camel case.
|
|
4
|
+
*/
|
|
2
5
|
export class CamelCaseNamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a camel case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The camel case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name) {
|
|
4
13
|
return name.slice(0, 1).toUpperCase() + name.slice(1, name.length);
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a camel case property name to a JSON property name.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} name - The camel case property name.
|
|
19
|
+
* @returns {string} - The JSON property name.
|
|
20
|
+
*/
|
|
6
21
|
toJsonName(name) {
|
|
7
22
|
return name.slice(0, 1).toLowerCase() + name.slice(1, name.length);
|
|
8
23
|
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for defining naming strategies for property names during serialization.
|
|
3
|
+
*/
|
|
1
4
|
export interface INamingStrategy {
|
|
5
|
+
/**
|
|
6
|
+
* Converts a JSON property name to a class property name.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} name - The JSON property name.
|
|
9
|
+
* @returns {string} - The class property name.
|
|
10
|
+
*/
|
|
2
11
|
fromJsonName: (name: string) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Converts a class property name to a JSON property name.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} name - The class property name.
|
|
16
|
+
* @returns {string} - The JSON property name.
|
|
17
|
+
*/
|
|
3
18
|
toJsonName: (name: string) => string;
|
|
4
19
|
}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import type { INamingStrategy } from "./INamingStrategy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to kebab case.
|
|
4
|
+
*/
|
|
2
5
|
export declare class KebabCaseNamingStrategy implements INamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a kebab case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The kebab case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a kebab case property name to a JSON property name.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} name - The kebab case property name.
|
|
17
|
+
* @returns {string} - The JSON property name.
|
|
18
|
+
*/
|
|
4
19
|
toJsonName(name: string): string;
|
|
5
20
|
}
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to kebab case.
|
|
4
|
+
*/
|
|
2
5
|
export class KebabCaseNamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a kebab case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The kebab case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name) {
|
|
4
13
|
return name.replace(/-\w/gu, (group) => group[1].toUpperCase());
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a kebab case property name to a JSON property name.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} name - The kebab case property name.
|
|
19
|
+
* @returns {string} - The JSON property name.
|
|
20
|
+
*/
|
|
6
21
|
toJsonName(name) {
|
|
7
22
|
return name
|
|
8
23
|
.split(/(?=[A-Z])/u)
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import type { INamingStrategy } from "./INamingStrategy.js";
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to Pascal case.
|
|
4
|
+
*/
|
|
2
5
|
export declare class PascalCaseNamingStrategy implements INamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a Pascal case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The Pascal case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a Pascal case property name to a JSON property name.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} name - The Pascal case property name.
|
|
17
|
+
* @returns {string} - The JSON property name.
|
|
18
|
+
*/
|
|
4
19
|
toJsonName(name: string): string;
|
|
5
20
|
}
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
|
+
/**
|
|
3
|
+
* Naming strategy for converting property names to Pascal case.
|
|
4
|
+
*/
|
|
2
5
|
export class PascalCaseNamingStrategy {
|
|
6
|
+
/**
|
|
7
|
+
* Converts a JSON property name to a Pascal case property name.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name - The JSON property name.
|
|
10
|
+
* @returns {string} - The Pascal case property name.
|
|
11
|
+
*/
|
|
3
12
|
fromJsonName(name) {
|
|
4
13
|
return name.slice(0, 1).toLowerCase() + name.slice(1, name.length);
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a Pascal case property name to a JSON property name.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} name - The Pascal case property name.
|
|
19
|
+
* @returns {string} - The JSON property name.
|
|
20
|
+
*/
|
|
6
21
|
toJsonName(name) {
|
|
7
22
|
return name.slice(0, 1).toUpperCase() + name.slice(1, name.length);
|
|
8
23
|
}
|
|
@@ -1 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a class instance to FormData for use in AJAX forms.
|
|
3
|
+
*
|
|
4
|
+
* @param {object} obj - The class instance to convert.
|
|
5
|
+
* @param {string} [formPrefix] - Optional prefix for form property names.
|
|
6
|
+
* @param {FormData} [formData] - Optional existing FormData to update.
|
|
7
|
+
* @returns {FormData} - The resulting FormData object.
|
|
8
|
+
*/
|
|
1
9
|
export declare const classToFormData: (obj: object, formPrefix?: string, formData?: FormData) => FormData;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
2
|
+
/* eslint-disable max-lines-per-function */
|
|
1
3
|
import { getPropertyName } from "./GetProperyName.js";
|
|
2
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Converts a class instance to FormData for use in AJAX forms.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} obj - The class instance to convert.
|
|
8
|
+
* @param {string} [formPrefix] - Optional prefix for form property names.
|
|
9
|
+
* @param {FormData} [formData] - Optional existing FormData to update.
|
|
10
|
+
* @returns {FormData} - The resulting FormData object.
|
|
11
|
+
*/
|
|
3
12
|
export const classToFormData = (obj, formPrefix, formData) => {
|
|
4
13
|
const newFormData = formData ?? new FormData();
|
|
5
14
|
const keys = Reflect.ownKeys(obj);
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import { SerializationSettings } from "../models/SerializationSettings.js";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the correct property name for serialization, considering decorators and settings.
|
|
4
|
+
*
|
|
5
|
+
* @param {object} obj - The object containing the property.
|
|
6
|
+
* @param {string} property - The source name of the property.
|
|
7
|
+
* @param {Partial<SerializationSettings>} [settings] - Optional serialization settings.
|
|
8
|
+
* @returns {string} - The correct property name for serialization.
|
|
9
|
+
*/
|
|
2
10
|
export declare const getPropertyName: (obj: object, property: string, settings?: Partial<SerializationSettings>) => string;
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
1
2
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
2
3
|
import { Serializable } from "../classes/Serializable.js";
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves the correct property name for serialization, considering decorators and settings.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} obj - The object containing the property.
|
|
8
|
+
* @param {string} property - The source name of the property.
|
|
9
|
+
* @param {Partial<SerializationSettings>} [settings] - Optional serialization settings.
|
|
10
|
+
* @returns {string} - The correct property name for serialization.
|
|
11
|
+
*/
|
|
4
12
|
export const getPropertyName = (obj, property, settings) => {
|
|
5
13
|
if (Reflect.hasMetadata("ts-serializable:jsonName", obj.constructor.prototype, property)) {
|
|
6
14
|
return Reflect.getMetadata("ts-serializable:jsonName", obj.constructor.prototype, property);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-serializable",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"author": "Eugene Labutin",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/LabEG/Serializable#readme",
|
|
@@ -37,14 +37,14 @@
|
|
|
37
37
|
"@commitlint/cli": "^19.8.0",
|
|
38
38
|
"@commitlint/config-conventional": "^19.8.0",
|
|
39
39
|
"@favware/cliff-jumper": "^6.0.0",
|
|
40
|
-
"@labeg/code-style": "^6.
|
|
40
|
+
"@labeg/code-style": "^6.3.0",
|
|
41
41
|
"@swc-node/register": "^1.10.10",
|
|
42
42
|
"@types/chai": "^5.2.1",
|
|
43
43
|
"chai": "^5.2.0",
|
|
44
44
|
"husky": "^9.1.7",
|
|
45
45
|
"lint-staged": "^15.5.0",
|
|
46
46
|
"reflect-metadata": "^0.2.2",
|
|
47
|
-
"typescript": "^5.8.
|
|
47
|
+
"typescript": "^5.8.3"
|
|
48
48
|
},
|
|
49
49
|
"keywords": [
|
|
50
50
|
"serialization",
|