Skip to content

Commit 5cbb320

Browse files
committed
feat!: drop support class mutation by jsonObject decorator
1 parent 3a350f1 commit 5cbb320

File tree

3 files changed

+4
-153
lines changed

3 files changed

+4
-153
lines changed

src/decorators/JsonObject.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
3-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
4-
/* eslint-disable @typescript-eslint/unbound-method */
5-
/* eslint-disable max-statements */
61
import type {SerializationSettings} from "../models/SerializationSettings.js";
7-
import {Serializable} from "../classes/Serializable.js";
8-
9-
export const jsonObject = (
10-
settings?: Partial<SerializationSettings>,
11-
extend?: boolean
12-
): ClassDecorator => (target: object): void => {
13-
if (extend === true) {
14-
Reflect.set(target, "defaultSettings", Serializable.defaultSettings);
15-
Reflect.set(target, "fromJSON", Serializable.fromJSON);
16-
17-
Reflect.set((target as any).prototype, "fromJSON", (Serializable.prototype as any).fromJSON);
18-
Reflect.set((target as any).prototype, "deserializeProperty", (Serializable.prototype as any).deserializeProperty);
19-
Reflect.set((target as any).prototype, "getJsonPropertyName", (Serializable.prototype as any).getJsonPropertyName);
20-
Reflect.set((target as any).prototype, "onWrongType", (Serializable.prototype as any).onWrongType);
21-
Reflect.set((target as any).prototype, "toJSON", (Serializable.prototype as any).toJSON);
22-
23-
Reflect.defineMetadata("ts-serializable:jsonObjectExtended", true, target);
24-
}
252

3+
export const jsonObject = (settings?: Partial<SerializationSettings>): ClassDecorator => (target: object): void => {
264
if (settings) {
275
Reflect.defineMetadata("ts-serializable:jsonObject", settings, target);
286
}

tests/decorators.spec.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,20 @@
1-
/* eslint-disable max-statements */
1+
22

33
import("reflect-metadata"); // Polyfill
44
import {assert} from "chai";
55
import {describe, it} from "node:test";
66
import type {User as IUser} from "./models/User";
7-
import type {FriendExt} from "./models/UserExt";
87

98
describe("Decorators", () => {
109
it("user property marked as jsonIgnore must by dropped", async () => {
1110
const {User} = await import("./models/User");
1211
const json: Record<string, unknown>[] = await import("./jsons/json-generator.json", {with: {type: "json"}});
12+
const [object] = Reflect.get(json, "default") as typeof json;
1313

14-
const user = new User().fromJSON(json[0]);
14+
const user = new User().fromJSON(object);
1515
user.isExpanded = true;
1616
const obj = JSON.parse(JSON.stringify(user)) as IUser;
1717

1818
assert.isUndefined(obj.isExpanded);
1919
});
20-
21-
it("class can be extended by decorator jsonObject", async () => {
22-
const {UserExt} = await import("./models/UserExt");
23-
const json = await import("./jsons/json-generator.json", {with: {type: "json"}});
24-
const [object] = Reflect.get(json, "default") as typeof json;
25-
26-
const user = new UserExt().fromJSON(object);
27-
28-
assert.isTrue(user instanceof UserExt);
29-
assert.strictEqual(user.id, object.id, "id is not equal");
30-
assert.strictEqual(user.index, object.index, "index is not equal");
31-
assert.strictEqual(user.guid, object.guid, "guid is not equal");
32-
assert.strictEqual(user.isActive, object.isActive, "isActive is not equal");
33-
assert.strictEqual(user.balance, object.balance, "balance is not equal");
34-
assert.strictEqual(user.picture, object.picture, "picture is not equal");
35-
assert.strictEqual(user.age, object.age, "age is not equal");
36-
assert.strictEqual(user.eyeColor, object.eyeColor, "eyeColor is not equal");
37-
assert.strictEqual(user.name, object.name, "name is not equal");
38-
assert.strictEqual(user.company, object.company, "company is not equal");
39-
assert.strictEqual(user.email, object.email, "email is not equal");
40-
assert.strictEqual(user.phone, object.phone, "phone is not equal");
41-
assert.strictEqual(user.address, object.address, "address is not equal");
42-
assert.strictEqual(user.about, object.about, "about is not equal");
43-
assert.strictEqual(user.latitude, object.latitude, "latitude is not equal");
44-
assert.strictEqual(user.longitude, object.longitude, "longitude is not equal");
45-
assert.deepEqual(user.tags, object.tags, "tags is not equal");
46-
assert.strictEqual(user.greeting, object.greeting, "greeting is not equal");
47-
assert.strictEqual(user.favoriteFruit, object.favoriteFruit, "favoriteFruit is not equal");
48-
49-
user.friends.forEach((friend: FriendExt, index: number) => {
50-
assert.strictEqual(friend.id, object.friends[index].id, `friend ${String(index)} id is not equal`);
51-
assert.strictEqual(friend.name, object.friends[index].name, `friend ${String(index)} name is not equal`);
52-
});
53-
});
5420
});

tests/models/UserExt.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)