tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

71 lignes · 3538 octetsBrut · Historique

ADR 0075: JSON is a package, packages/json

Status: Proposed (2026-11-03); needs engineering review (#44).

Context

  • There is no JSON. std has MessagePack (std/msgpack, ADR 0051) but nothing for JSON, so apps work around it. toki2 wrote infra/web/flat_json, which reads one flat object of strings and whole numbers, has no escapes and no encoder. A JSON HTTP API (Starfleet API, a SpaceTraders-style game) needs both directions, on untrusted bodies.
  • Where it goes. std/msgpack is in std because the runtime and the event store examples lean on it. JSON is a web format. Its API (value shape, number model, decoder style) is the kind of thing that changes in its first months, and std is versioned with the compiler.
  • ADR 0019's derives only consume. @derivable fits an encoder (fn to_json(x: a) -> Value) but not a decoder, the same gap ADR 0051 records for FromMsgPack.

Decision

  • A package, not std: packages/json, depended on by a git tag like uuid and web. It is pure Louise over Bytes, so it needs no externs and runs in both backends. Moving it into std later stays possible if it settles.
  • Value: Null | Bool | Int | Float | String | Array(List) | Object(List<(String, Value)>).
    • Integers are exact (a big Int), which JavaScript's Number is not. A client that needs more than 2^53 must use a big-number reader.
    • Objects keep their order and refuse duplicate keys. Order is what makes write deterministic (tests, docs, signatures over a body). Refusing duplicates closes the "which "admin" wins" ambiguity between parsers.
  • parse is hardened like std/msgpack.read:
    • every error carries a byte offset;
    • depth is capped at 512;
    • escapes are checked, and lone surrogates refused;
    • control characters in strings are refused;
    • the RFC number grammar is checked before float.parse, which on its own accepts inf and .5;
    • a number that overflows a Float is refused.
  • write and pretty:
    • Escape only what RFC 8259 requires.
    • A non-finite Float panics, as msgpack.write panics on an Int outside int64: silently writing null would change the data.
  • @derive(ToJson) uses ToMsgPack's convention (a record becomes an object, a positional constructor a {Name: [...]} object, a constructor without fields its name), so one type reads the same in both formats. Option is null or the value, which is what JSON APIs expect.
  • Decoding uses decoder combinators (json/decode), not a FromJson trait:
    • field name inner next takes the rest of the decoder as a continuation, so a record decoder is one use line per field.
    • Errors carry the path (Mismatch(path, expected, found), Missing(path, name), shown as $.ships.1: …), which an API can hand straight back in a 422.
    • A trait would need one impl per type anyway (no producer derives) and could not take parameters (a different decoder for one endpoint).

Consequences

  • Apps replace their hand-written JSON (toki2's flat_json).
  • When ADR 0051's producer derives land, a @derive(FromJson) could build json/decode decoders. That would be additive.
  • CI checks, tests and natively tests the package with the others (scripts/ci.sh, packages part).
  • Not done here:
    • streaming (SAX-style) parsing;
    • a JSON Pointer or path query API;
    • escaping of U+2028 and U+2029 for embedding in <script>, which is the HTML layer's job.