json-as
Fast, type-safe JSON for AssemblyScript.
GitHub · npm — npm install json-as
json-as is a compiler transform: mark a class @json and it generates specialized serialize/deserialize code at build time. No runtime reflection, no hand-written schemas — just JSON.parse<T> and JSON.stringify, reaching multi-GB/s throughput with SIMD/SWAR scanning.
ts
import { JSON } from "json-as";
@json
class Vec3 {
x: f32 = 0;
y: f32 = 0;
z: f32 = 0;
}
const v = JSON.parse<Vec3>('{"x":1.5,"y":2.5,"z":3.5}');
JSON.stringify(v); // '{"x":1.5,"y":2.5,"z":3.5}'Highlights
- Typed —
JSON.parse<T>/JSON.stringifyover your own@jsonclasses, nested to any depth. - Batteries included — strings, all number types,
bool,Date, arrays,Map,Set, typed arrays,ArrayBuffer. - Dynamic when you need it —
JSON.Value,JSON.Obj, andJSON.Rawfor unknown or open-ended shapes. - Lazy fields — defer parsing of fields you rarely read; untouched fields pass through their raw bytes on serialize.
- Custom formats —
@serializer/@deserializerfor compact or bespoke wire formats. - Fast — three interchangeable scanning modes (SWAR, SIMD, NAIVE) selected at build time.
Guide
- Getting Started — install, enable the transform, first round-trip
- Basic Usage — fields, nesting, aliases, optional fields, nullability
- Built-in Types — dates, collections, typed arrays
- Custom Serialization —
@serializer/@deserializer - Built-in Subclasses — subclassing
Array,Map, typed arrays - Lazy Fields — on-demand field parsing
- Performance — modes, the fast path, and benchmarks
Reference
- Decorators — every decorator in one place
- Dynamic Types —
JSON.Value/JSON.Obj/JSON.Raw - Configuration — build-time
JSON_*environment variables - Runtime Behavior — the shared buffer,
JSON.internal,JSON.Memory
Deep Dive
How the interesting parts actually work:
- Generated Code & the Fast Path — what
@jsoncompiles to - SWAR & SIMD Scanning — finding structure many lanes at a time
- The Lazy Slot — the packed
u64behind lazy fields - The Serialization Buffer — zero-copy, zero-alloc round-trips
Testing your AssemblyScript too? See as-test.
