Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 25, 2024
1 parent ca9fbec commit 6e34325
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,16 @@ const serialized = JSON.stringify(arr);
const parsed = JSON.parse<Base[]>(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<Base[]>(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!

Expand Down
58 changes: 36 additions & 22 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -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>(player);

const parsed = JSON.parse<Player>(stringified);

console.log(stringified);
console.log(JSON.stringify(parsed))

0 comments on commit 6e34325

Please sign in to comment.