██ ███████ ██████ ███ ██ █████ ███████
██ ██ ██ ██ ████ ██ ██ ██ ██
██ ███████ ██ ██ ██ ██ ██ █████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
█████ ███████ ██████ ██ ████ ██ ██ ███████
AssemblyScript - v1.0.0-alpha.3
npm install [email protected]
Add the --transform
to your asc
command (e.g. in package.json)
--transform json-as
Alternatively, add it to your asconfig.json
{
// ...
"options": {
"transform": ["json-as/transform"]
}
}
If you'd like to see the code that the transform generates, run with JSON_DEBUG=true
import { JSON } from "json-as";
@json
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
}
@json
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((age) => age < 18)
@omitif('this.age <= 0')
age!: i32;
@omitnull()
pos!: Vec3 | null;
isVerified!: boolean;
}
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 serialized = JSON.stringify<Player>(player);
const parsed = JSON.parse<Player>(stringified);
console.log("Serialized: " + stringified);
console.log("Parsed: " + parsed);
Classes can even have inheritance. Here's a nasty example
@json
class Base {}
@json
class Vec1 extends Base {
x: f32 = 1.0;
}
@json
class Vec2 extends Vec1 {
y: f32 = 2.0;
}
@json
class Vec3 extends Vec2 {
z: f32 = 3.0;
}
const arr: Base[] = [
new Vec1(),
new Vec2(),
new Vec3()
];
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
{
// ...
"options": {
"transform": ["json-as/transform"]
}
}
If you use this project in your codebase, consider dropping a star. I would really appreciate it!
This project is distributed under an open source license. You can view the full license using the following link: License
Please send all issues to GitHub Issues and to converse, please send me an email at [email protected]
- Email: Send me inquiries, questions, or requests at [email protected]
- GitHub: Visit the official GitHub repository Here
- Website: Visit my official website at jairus.dev
- Discord: Converse with me on My Discord or on the AssemblyScript Discord Server