Getting Started
Installation
bash
npm install json-asUse the transform when compiling:
bash
asc assembly/index.ts --transform json-asOptional performance flags:
bash
asc assembly/index.ts --transform json-as --enable simdOr configure it in asconfig.json:
json
{
"options": {
"transform": ["json-as"]
}
}If you want to inspect generated code during development:
bash
JSON_WRITE=assembly/test.ts asc assembly/test.ts --transform ./transformFirst Example
ts
import { JSON } from "json-as";
@json
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
}
const input = '{"x":1.5,"y":2.5,"z":3.5}';
const parsed = JSON.parse<Vec3>(input);
const encoded = JSON.stringify(parsed);What @json does
json-as is transform-driven. The @json decorator tells the transform to generate serialization and deserialization methods for the class.
That gives you:
- typed object parsing with
JSON.parse<T>(...) - typed serialization with
JSON.stringify(...) - field-level handling for nested classes, arrays, maps, sets, and strings
For dynamic or unknown JSON, use JSON.Value or JSON.Obj instead of @json.
