json-as 2.1
Type-safe JSON for AssemblyScript, compiled for your models.
Mark a class with @json, enable the compiler transform, and use JSON.parse<T> and JSON.stringify. The transform generates readers and writers at build time; your application needs no runtime schema or reflection.
import { JSON } from "json-as";
@json
class Point {
x: f32 = 0;
y: f32 = 0;
label: string = "";
}
function readX(input: string): f32 {
const point = JSON.parse<Point>(input);
return point.x;
}Lazy parsing is enabled by default in 2.1. Visible parse sites can skip decoding fields proven unused. Here, the reader can scan past y and label while decoding x. Full reads and uncertain uses retain eager behavior. This optimization creates no lazy getters or saved source ranges for ordinary fields.
Version
These docs cover 2.1.0. For installation before its npm publication, use the packed-source instructions. The upgrade guide covers the changes from 2.0.
Start here
- Getting started — installation, compiler setup, and a first round-trip.
- Upgrading to 2.1 — one lazy API, eager controls, and migration from deferred fields.
- Basic usage — fields, aliases, defaults, nullability, and reuse.
- Built-in types — strings, numbers, dates, arrays, maps, sets, and buffers.
- Lazy parsing — compile-time field selection and eager controls.
- Custom serialization — custom wire formats.
- Built-in subclasses — native collection semantics.
- Performance — 2.1 charts and measurement conditions.
Reference
JSON API:
parse,stringify, andvalidate.Dynamic types:
JSON.Value,JSON.Obj,JSON.Arr, andJSON.Raw.Configuration: compile-time options.
Runtime behavior: output ownership, reuse, and nested operations.
Implementation
Upgrading from 1.x? Read the 2.0 migration notes too.
GitHub · npm · MPL-2.0 license
