Skip to content

Commit 6b7c61f

Browse files
committed
feat: add toJSON function and refactor Serializable class to utilize it
1 parent 5911807 commit 6b7c61f

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

src/classes/Serializable.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-prototype-builtins */
1+
22
/* eslint-disable @typescript-eslint/no-unsafe-argument */
33

44
import {deserializeProperty} from "../functions/DeserializeProperty.js";
@@ -7,6 +7,7 @@ import {SerializationSettings} from "../models/SerializationSettings.js";
77
import {classToFormData} from "../functions/ClassToFormData.js";
88
import {getPropertyName} from "../functions/GetPropertyName.js";
99
import {fromJSON} from "../functions/FromJSON.js";
10+
import {toJSON} from "../functions/ToJSON.js";
1011

1112
/**
1213
* Class that helps you deserialize objects to classes.
@@ -98,24 +99,7 @@ export class Serializable {
9899
* @memberof Serializable
99100
*/
100101
public toJSON (): Record<string, unknown> {
101-
const toJson: Record<string, unknown> = {};
102-
const keys = Reflect.ownKeys(this);
103-
104-
for (const key of keys) {
105-
if (typeof key === "symbol") {
106-
// eslint-disable-next-line no-continue
107-
continue;
108-
}
109-
110-
if (this.hasOwnProperty(key)) {
111-
if (Reflect.getMetadata("ts-serializable:jsonIgnore", this.constructor.prototype, key) !== true) {
112-
const toProp = this.getJsonPropertyName(key);
113-
Reflect.set(toJson, toProp, Reflect.get(this, key));
114-
}
115-
}
116-
}
117-
118-
return toJson;
102+
return toJSON(this);
119103
}
120104

121105
/**

src/functions/DeserializeProperty.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Serializable} from "../classes/Serializable";
22
import {AcceptedTypes} from "../models/AcceptedType";
33
import {SerializationSettings} from "../models/SerializationSettings";
4+
import {fromJSON} from "./FromJSON";
45
import {onWrongType} from "./OnWrongType";
56

67
/**
@@ -103,15 +104,22 @@ export const deserializeProperty = (
103104
!Array.isArray(acceptedType) &&
104105
(
105106
acceptedType.prototype instanceof Serializable ||
106-
Boolean(Reflect.getMetadata("ts-serializable:jsonObjectExtended", acceptedType))
107+
acceptedType instanceof Function
107108
) &&
108109
jsonValue !== null &&
109110
jsonValue !== void 0 &&
110111
typeof jsonValue === "object" && !Array.isArray(jsonValue)
111112
) {
112-
const TypeConstructor: new () => Serializable = acceptedType as new () => Serializable;
113+
if (acceptedType.prototype instanceof Serializable) {
114+
const TypeConstructor = acceptedType as new () => Serializable;
113115

114-
return new TypeConstructor().fromJSON(jsonValue, settings);
116+
return new TypeConstructor().fromJSON(jsonValue, settings);
117+
}
118+
119+
// Class without Serializable base class
120+
const TypeConstructor = acceptedType as new () => object;
121+
122+
return fromJSON(new TypeConstructor(), jsonValue, settings);
115123
} else if (// Instance any other class, not Serializable, for parsing from other class instances
116124
acceptedType instanceof Function &&
117125
jsonValue instanceof acceptedType

src/functions/ToJSON.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
2+
/* eslint-disable no-prototype-builtins */
3+
4+
import {Serializable} from "../classes/Serializable";
5+
import {getPropertyName} from "./GetPropertyName";
6+
7+
export const toJSON = (obj: Serializable | object): Record<string, unknown> => {
8+
const toJson: Record<string, unknown> = {};
9+
const keys = Reflect.ownKeys(obj);
10+
11+
for (const key of keys) {
12+
if (typeof key === "symbol") {
13+
// eslint-disable-next-line no-continue
14+
continue;
15+
}
16+
17+
if (obj.hasOwnProperty(key)) {
18+
if (Reflect.getMetadata("ts-serializable:jsonIgnore", obj.constructor.prototype, key) !== true) {
19+
const toProp = obj instanceof Serializable ?
20+
obj.getJsonPropertyName(key) :
21+
getPropertyName(obj, key);
22+
Reflect.set(toJson, toProp, Reflect.get(obj, key));
23+
}
24+
}
25+
}
26+
27+
return toJson;
28+
};

0 commit comments

Comments
 (0)