JSON
Hale provides two ways to work with JSON: compile-time generated struct codecs (Type::from_json and Type::to_json) for fixed schemas, and the std::json…
Hale provides two ways to work with JSON: compile-time generated struct codecs (Type::from_json and Type::to_json) for fixed schemas, and the std::json module for dynamic field extraction, streaming output construction, cursor iteration, and input validation.
type Order { id: Int `json:"id"`; price: Int `json:"px"`; qty: Float `json:"sz"`; active: Bool `json:"on"`; side: String `json:"side"`; currency: String = "USD";}
fn parse_and_echo(body: String) -> String fallible(JsonError) { let order = Order::from_json(body) or raise; return Order::to_json(order);}
fn main() { let raw = "{\"id\":7,\"px\":100,\"sz\":2.5,\"on\":true,\"side\":\"buy\"}"; let out = parse_and_echo(raw) or ""; println(out);}API Summary
Section titled “API Summary”| API | Signature / Type | Description |
|---|---|---|
Type::from_json |
fn(String) -> Type fallible(JsonError) |
Single-pass parser generated for structs with json:"..." field tags |
Type::to_json |
fn(Type) -> String |
Infallible serializer generated for structs with json:"..." field tags |
std::json::string_field |
fn(String, String) -> JsonString |
Inspects a top-level field’s JSON type (kind) and decoded string (text) |
std::json::find_string_field |
fn(String, String) -> String |
Extracts a top-level field as String (returns "" if missing; coerces non-strings) |
std::json::find_int_field |
fn(String, String) -> Int |
Extracts a top-level field as Int (returns 0 if missing or non-numeric) |
std::json::find_bool_field |
fn(String, String) -> Bool |
Extracts a top-level field as Bool (returns false unless true) |
std::json::find_field_raw |
fn(String, String) -> String |
Returns the raw JSON substring of a top-level field (for nested objects/arrays) |
std::json::array_first / array_next |
ArrayIterSpan cursor |
Iterates elements of a JSON array (it.element, it.done) |
std::json::object_first / obj_key_string |
ObjectIterSpan cursor |
Iterates key-value pairs of a JSON object with dynamic keys |
std::json::Builder |
Streaming builder locus | Incrementally constructs well-formed JSON objects and arrays |
std::json::escape_string / unescape_string |
fn(String) -> String |
Escapes and unescapes JSON string content (including \uHHHH and surrogate pairs) |
std::json::valid |
fn(String) -> Bool |
Zero-allocation strict JSON validator (up to 64 nesting levels) |
std::json::valid_object |
fn(String) -> Bool |
Validates a JSON object with up to 64 unique, unescaped top-level keys |
Typed Struct Codecs (from_json and to_json)
Section titled “Typed Struct Codecs (from_json and to_json)”When parsing or serializing a known schema, annotate type fields with `json:"<key>"` metadata tags. The compiler generates a single-pass parser (Type::from_json) and serializer (Type::to_json). If a field omits the json: tag, the field’s identifier is used as the JSON key.
type Addr { city: String `json:"city"`; zip: Int `json:"zip"`;}
type Person { name: String `json:"name"`; home: Addr `json:"home"`; role: String = "member"; // optional: uses default when key is absent}
let p = Person::from_json(body) or raise;println(p.home.city);
let encoded = Person::to_json(p);let roundtrip = Person::from_json(encoded) or raise;Decoding & Encoding Rules
Section titled “Decoding & Encoding Rules”-
Single-pass scanning:
Type::from_json(s) -> Type fallible(JsonError)walks the JSON object once, dispatches matched keys to their target fields, and skips unknown keys (including nested objects or arrays under unknown keys). -
Required vs. optional fields: A missing field raises
JsonError { kind: "missing_field", field }(naming the missing field) unless the field declaration includes a default value, of any shape a field default may have: a literal (role: String = "member",n: Int = -1), an enum variant (side: Side = Side::Buy), a record literal (inst: Instrument = Instrument { sym: "Y", tick: 2 }, its own fields’ defaults filling what it leaves out). The codec builds the default when the key is absent, exactly as the record’s own literal would. A key sent asnullis a key absent: the field takes that same default, whatever its shape ("n": nullreads-1,"side": nullreadsSide::Buy,"inst": nullreads the declaredInstrument { sym: "Y", tick: 2 }), and a field with no default ismissing_field. Only astd::json::Valuefield holdsnullas its text. -
Optional fields: a field declared
x?: T(Records, Optional Fields) may be left out or sent asnull: itshas_xflag says which. The encoder writesxonly whenhas_xis set, and an optionalstd::json::Valueholdingnullis left out rather than written asnull.x?: T clearablereadsnullas itsclear_xflag and writes it back asnull. -
Error handling: Because
from_jsonisfallible(JsonError), callers must address errors withor raise,or <fallback>, or an error handler.Type::to_json(value)is infallible and always returns aString. -
Supported field types: what an api surface’s JSON codec carries of a field (The API surface):
- Scalars:
Int,U64, the fixed-width integers (I32…U8),Float,Bool,String, andDecimal<S>; a unit-dialect scalar (an identity, a range, a quantity) as the integer it counts (anInt, or aU64as aU64is); aTimeas its RFC 3339 text; aDurationas its nanoseconds; astd::json::Valueas its text. - Exact decimals:
Decimal<S>serializes as an exact JSON number (19.99, without float approximation) and parses at scaleS, any JSON number spelling included: an exponent is applied exactly (1e-7at scale 8 is0.0000001,1.50E+1is15). A JSON string holding a number parses the same way, so an API that quotes its prices ({"price": "97000.50"}) and one that sends numbers both decode, with no float on the way. Numbers with more fractional digits thanS(the exponent applied), a mantissa past i128, and spellings that are not JSON numbers (+1,.5,1.,01, or a string holding one of those, a space or anything but the number) are rejected withwrong_typerather than truncated or read as zero. To write a Decimal as a string, tag the field`json:"price,string"`(or`json:",string"`to keep the field’s name):to_jsonthen writes"97000.5". Thestringoption is a Decimal’s (a field or a list of them). - Unsigned 64-bit integers: a
U64field is always written as a JSON string of its decimal digits ("18446744073709551615"),0xhex under`json:",hex"`, because a JavaScript reader’s numbers are exact only through2^53. It reads a JSON string of decimal digits or0xhex, or a JSON integer that fits; a sign, a fraction, an exponent or a value past2^64 - 1iswrong_type. - 256-bit integers: a
U256orI256field reads a JSON string of decimal digits or0xhex (either case; anI256’s with a leading-), or a JSON integer that fits, and writes a JSON string of its decimal digits. Tagged`json:",hex"`it writes0xhex instead ("0xde0b6b3a7640000"). A fraction, an exponent, a value out of range or a string with anything else in it iswrong_type. - Hex quantities: an
Intfield tagged`json:"gasUsed,hex"`reads JSON-RPC’s quoted hex ("0x4dc") as well as a plain number, and writes"0x4dc"; a list ofInts takes the option for each element. No0x, no digits, a sign, a space or a value pastInt’s range iswrong_type. A negativeInthas no hex text, so it is written as its JSON number, which reads back. The options arestringandhex; the check refuses any other, and either one on a field it is not for. - Enums: a payload-free variant is its name as a JSON string (
"Buy"), read back by name; a name no variant has iswrong_typefor the field. A variant with values is written as the api codec writes it ({"Circle": 1.5}). A variant takes a tag too:Buy `json:"buy"`crosses as"buy"(and"Buy"is no longer read),Fill(Int) `json:"fill"`as{"fill": 3};Other(String) `json:",other"`catches any string no variant names and writes it back, and a payload-freeUnknown `json:",other"`catches it without keeping it.@wire_case(snake)on the enum’s declaration (orscreaming_snake,kebab,lower) spells every untagged variant that way, andE::name/E::parseread the same names (Records & Data). See Wire names and the catch-all. - Nested records: Any field whose type is another record of the seed is parsed and serialized recursively, by that record’s own codec (its keys its fields’ tags, else their names), whether or not it declares a
json:tag of its own. - Lists: a
bounded[T; N]or[T; N]of any of those is a JSON array; more elements thanNistoo_long, fewer than a fixed array’stoo_short, and an absentboundedfield is empty. - Maps: a
bounded[T; N] indexed_by kof records is a JSON object keyed by each record’sk, the record without its key as the value ({"AAPL": {"rows": []}}). A map that is not an object, or a member that is not a record (an array where the record’s object belongs), iswrong_typefor the field, never an empty map.
- Scalars:
-
Positional rows: many APIs send a table’s rows as arrays (
[[1688666400, "2990.5", "3010", 100.5], ...]). Mark the row’s record@positionaland it crosses as an array of its fields in declaration order, as a field, a list’s element or a whole value;@positionalgives the recordfrom_json/to_jsonwithout a tag:@positional type Candle { t: Int; open: Decimal<2>; close: Decimal<2>; vol: Decimal<4>; }@positional type Level { px: Decimal<2>; qty: Decimal<4>; orders: Int = 1; }type Book { sym: String `json:"symbol"`; bids: bounded[Level; 50]; candles: bounded[Candle; 500]; }let book = Book::from_json(body) or raise; // {"symbol": "X", "bids": [["3000.40", "0.5", 1], ...], ...}Each element is read as its field would be, so one row mixes kinds (a Decimal from a string beside an
Intfrom a number). Elements past the last field are ignored, as unknown keys are, so a row that gains a column keeps decoding; a row that ends early leaves the rest absent: a field with a default takes it, and any other ismissing_fieldnamed<Type>.<field>(Level.qty), the way every error inside a row names its field. Every field is an element (no presence pairs), and a positional record’s tags name no key. A servedapisurface does not carry one: read and write positional rows with the codec calls. -
Through an import:
pricing::Observation::from_json(text)andpricing::Observation::to_json(v)name the library’s declaration, aspricing::Observation { … }does; the library’s ownObservation::to_jsonis the same codec. -
The check is the gate: a tagged record with a field the codec does not carry (
Bytes, an unbounded[T], a tuple), in it or in a record it reaches, has no codec, andhale checkrefuses the call naming the field:`Export::from_json`: `Export` has a field `raw: Bytes`, which the JSON codec does not carry. A call on a record with nojson:tag is refused with the tag to write, and a call on an enum, or with two arguments, is refused too. A program that checks builds.
Dynamic Field Extraction (std::json)
Section titled “Dynamic Field Extraction (std::json)”For ad-hoc queries or configuration fragments where you only need a few fields, extract fields directly by key name:
let doc = "{\"name\": \"Ada\", \"age\": 36, \"active\": true}";
let name = std::json::find_string_field(doc, "name"); // "Ada"let age = std::json::find_int_field(doc, "age"); // 36let active = std::json::find_bool_field(doc, "active"); // true- Top-level matching: Field lookups match top-level keys only. A string value elsewhere in the document cannot shadow a key, and nested keys are not matched until you extract their parent object.
- Zero-value defaults: Missing fields return the target type’s zero value (
"",0,false). - Nested objects: Call
std::json::find_field_rawto retrieve the raw JSON substring of a nested object, then query fields on that substring:
let inner = std::json::find_field_raw(doc, "address");let city = std::json::find_string_field(inner, "city");Handling Nullable and Optional Fields (string_field)
Section titled “Handling Nullable and Optional Fields (string_field)”find_string_field is permissive and coerces non-string tokens into strings, while find_int_field and find_bool_field coerce null or mismatched types to 0 and false:
| Input JSON | find_string_field(doc, "owner") |
string_field(doc, "owner") |
|---|---|---|
{"owner": "Ada"} |
"Ada" |
JsonString { kind: "string", text: "Ada" } |
{"owner": null} |
"null" |
JsonString { kind: "null", text: "" } |
{"owner": ""} |
"" |
JsonString { kind: "string", text: "" } |
{} |
"" |
JsonString { kind: "missing", text: "" } |
{"owner": 7} |
"7" |
JsonString { kind: "number", text: "" } |
When you need to distinguish "null" from null, or verify a field’s type before reading it, use std::json::string_field(doc, key) -> std::json::JsonString:
let owner = std::json::string_field(body, "owner");if owner.kind == "string" { println("owned by ", owner.text);} else if owner.kind == "null" || owner.kind == "missing" { println("unassigned");} else { println("owner must be a string or null");}JsonString contains two fields:
kind: One of"string","null","missing","number","bool","array","object", or"invalid".text: Decoded string content (quotes stripped, escapes resolved) whenkind == "string", and""for all other kinds.
"missing" means the document is a JSON object without that key. "invalid" means the document is not an object or the field value is syntactically malformed (such as an unterminated string or bare NaN). Duplicate keys within the same object resolve to the first occurrence.
You can also use string_field to guard numeric or boolean reads:
if std::json::string_field(body, "port").kind == "number" { let port = std::json::find_int_field(body, "port"); println(port);}JsonString, ArrayIterSpan, ObjectIterSpan, and JsonFieldRange are statically checked structs in std::json, so misspelled field names fail at compile time.
Iterating Arrays and Dynamic Objects
Section titled “Iterating Arrays and Dynamic Objects”Array Iteration (ArrayIterSpan)
Section titled “Array Iteration (ArrayIterSpan)”Walk a JSON array using std::json::array_first and std::json::array_next:
let arr = "[10, 20, 30]";let mut it = std::json::array_first(arr);while !it.done { let n = std::str::parse_int(it.element) or 0; println(n); it = std::json::array_next(it);}array_first returns an ArrayIterSpan containing the first element substring and done: Bool. array_next advances the cursor.
Dynamic Object Iteration (ObjectIterSpan)
Section titled “Dynamic Object Iteration (ObjectIterSpan)”When iterating a JSON object whose keys are not known ahead of time (such as a dependencies or versions map) with object_first, read each key using std::json::obj_key_string(it, doc) and values using obj_value_string. Unlike manual slicing (doc[it.key_start..it.key_end]), obj_key_string decodes JSON escape sequences in keys. To compare a key without allocating, use obj_key_eq.
Building JSON (std::json::Builder)
Section titled “Building JSON (std::json::Builder)”std::json::Builder is a streaming JSON writer that tracks open object/array scopes, escapes strings, and inserts commas automatically into a single growing byte buffer:
let b = std::json::Builder { };b.begin_object();b.string_field("name", "Ada");b.int_field("age", 36);b.bool_field("active", true);b.end_object();let out = b.result(); // {"name": "Ada", "age": 36, "active": true}| Builder Method | Description |
|---|---|
begin_object() / end_object() |
Opens and closes a JSON object ({ … }) |
begin_array() / end_array() |
Opens and closes a JSON array ([ … ]) |
begin_object_field(name) |
Writes "name": { and opens a nested object scope |
begin_array_field(name) |
Writes "name": [ and opens a nested array scope |
string_field(name, value) / field(name, value) |
Writes "name": "escaped_value" |
int_field(name, value) |
Writes "name": 123 |
bool_field(name, value) |
Writes "name": true or false |
null_field(name) |
Writes "name": null |
string_value(v) / value(v) |
Writes a quoted, escaped string element inside an array |
int_value(n) / bool_value(b) / null_value() |
Writes a number, true/false or null element inside an array |
raw_field(name, json) |
Writes "name": <json>, the value as given, for JSON you encoded already |
raw_value(json) |
Writes an array element as given, for JSON you encoded already |
result() -> String |
Returns an independent copy of the accumulated JSON string |
field(name, value) and value(v) take a string and write it quoted and
escaped, as string_field / string_value do (since v0.35; before, they
wrote the argument raw and warned deprecated:). Use raw_field /
raw_value for JSON you encoded already (a number you formatted, a
nested document, an array from build_array).
The Builder separates with ": " and ", ". The spacing is part of
what it writes, and programs that store its documents and match them as
text rely on it, so it does not change.
String Escaping and Unescaping
Section titled “String Escaping and Unescaping”For manual string escaping, use std::json::escape_string(s) and std::json::unescape_string(s):
let a = std::json::unescape_string("hale.v\\u0031");println(a == "hale.v1"); // trueprintln(std::json::unescape_string("\\u00e9")); // éprintln(std::json::unescape_string("\\u4e2d")); // 中println(std::json::unescape_string("\\ud83d\\ude00")); // 😀unescape_string resolves standard escapes (\", \\, \n, \t, etc.), \uHHHH hex escapes, and UTF-16 surrogate pairs for code points above U+FFFF.
- Unpaired surrogates and
\u0000decode to `` (U+FFFD). Because Hale strings are NUL-terminated internally, replacing\u0000withU+FFFDprevents silent string truncation. Both are valid JSON, sovalidandvalid_objectaccept them, and every field reader (find_string_field,string_field, the object cursor,from_json) decodes them the same way. - Unrecognized escapes (such as
\uZZZZ) pass through unchanged.
Validating Untrusted Input
Section titled “Validating Untrusted Input”Because find_*_field helpers are forgiving by design, validate untrusted payloads at system boundaries before extracting fields:
if !std::json::valid(body) { return Response { status: 400, body: "not JSON" };}
if !std::json::valid_object(row) { return Response { status: 400, body: "expected flat record object" };}| Validator | Guarantees & Limits |
|---|---|
std::json::valid(doc) |
Exactly one well-formed JSON value (RFC 8259 numbers: rejects 01, 1., .5, 1e, +1; accepts -0, 1e5, -1.25e-3), valid UTF-8, no trailing tokens (any \uXXXX escape is accepted, \u0000 and lone surrogates included), maximum nesting depth of 64 levels. Zero heap allocations (~10 ms/MB). |
std::json::valid_object(doc) |
All guarantees of valid(doc), plus the top-level value must be an object with at most 64 top-level keys, all keys must be unique, and no key may contain escape sequences (preventing "\u0061" vs "a" lookup mismatches). |
Next: HTTP Servers & Clients.