From 6e343254d0d97f946bc7116f25fa2f6aa6fcb2ae Mon Sep 17 00:00:00 2001 From: Jairus Date: Thu, 25 Jul 2024 18:50:20 -0400 Subject: [PATCH] fix tests --- README.md | 21 +++++------------- assembly/test.ts | 58 ++++++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index cb3dc8c..0509f44 100644 --- a/README.md +++ b/README.md @@ -107,25 +107,16 @@ const serialized = JSON.stringify(arr); const parsed = JSON.parse(serialized); ``` -Classes can even have inheritance. Here's a nasty example - -```js -@json -class Base {} - -const serialized = JSON.stringify(arr); -// [{"x":1.0},{"x":1.0,"y":2.0},{"y":2.0,"x":1.0,"z":3.0}] -const parsed = JSON.parse(serialized); -``` - You can also add it to your `asconfig.json` +```json { -// ... -"options": { -"transform": ["json-as/transform"] -} + // ... + "options": { + "transform": ["json-as/transform"] + } } +```` If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it! diff --git a/assembly/test.ts b/assembly/test.ts index d20572f..f5d8629 100644 --- a/assembly/test.ts +++ b/assembly/test.ts @@ -1,29 +1,43 @@ // import { JSON } from "."; import { JSON } from "."; -@json -class Message { - @alias("raw_foo") - public raw: JSON.Raw = "[1,2,3]"; - constructor(role: string, content: string) { - this._role = role; - this.content = content; - } - - @alias("role") - protected _role: string; - get role(): string { - return this._role; - } - - content: string; +@json +class Vec3 { + x: f32 = 0.0; + y: f32 = 0.0; + z: f32 = 0.0; } @json -class UserMessage extends Message { - constructor(content: string) { - super("user", content); - } +class Player { + @alias("first name") + firstName!: string; + lastName!: string; + lastActive!: i32[]; + // Drop in a code block, function, or expression that evaluates to a boolean + @omitif("this.age < 18") + age!: i32; + @omitnull() + pos!: Vec3 | null; + isVerified!: boolean; } -console.log(JSON.stringify(new Message("user", "foo"))); -console.log(JSON.stringify(new UserMessage("foo"))); + +const player: Player = { + firstName: "Emmet", + lastName: "West", + lastActive: [8, 27, 2022], + age: 23, + pos: { + x: 3.4, + y: 1.2, + z: 8.3 + }, + isVerified: true +}; + +const stringified = JSON.stringify(player); + +const parsed = JSON.parse(stringified); + +console.log(stringified); +console.log(JSON.stringify(parsed)) \ No newline at end of file