UNPKG

json-as

Version:

The only JSON library you'll need for AssemblyScript. SIMD enabled

45 lines (35 loc) 794 B
import { JSON } from "."; @json class Vec3 { x: f32 = 0.0; y: f32 = 0.0; z: f32 = 0.0; } @json class Player { @alias("first name") firstName!: string | null; lastName!: string; lastActive!: i32[]; // Drop in a code block, function, or expression that evaluates to a boolean @omitif((self: Player) => self.age < 18) age!: i32; pos!: Vec3 | null; isVerified!: boolean; } const player: Player = { firstName: "Jairus", lastName: "Tanaka", lastActive: [3, 9, 2025], age: 18, pos: { x: 3.4, y: 1.2, z: 8.3, }, isVerified: true, }; const serialized = JSON.stringify<Player>(player); const deserialized = JSON.parse<Player>(serialized); console.log("Serialized " + serialized); console.log("Deserialized " + JSON.stringify(deserialized));