ADR-0294:
Unified json.lib.Value Builders &
codec derive JsonValue
- Status: Accepted
- Date: 2026-11-28
- Scope:
plugins/json/,plugins/codec/
Context
Tenon provides two complementary JSON layers:
json.lib(plugins/json/lib.tn), which defines the canonical recursiveValueAST (Null | Bool(bool) | Num(i64) | Float(f64) | Str(str) | Arr(vec<Value>) | Obj(map<str, Value>)) with exact 64-bit integer fidelity distinct fromf64.codec.json(plugins/codec/json.tn), aGEN_ABI 2generator (derive Json) that previously emitted only wire-string/bytes serializers (<type>_to_json/<type>_from_json) without directjson.lib.Valueconversion.
Callers bridging typed structs/enums and dynamic
json.lib.Value trees (such as MCP payloads, state
deltas, and action projections) had to either serialize and
re-parse intermediate JSON strings or hand-write verbose
map().set(...) boilerplate.
Decision
Preserve One Canonical
json.lib.ValueAST with Zero Breakage- Keep
json.lib.Valueas the single canonical 7-variant enum, retainingValue.Num(i64)alongsideValue.Float(f64)so all existing exhaustive matchers (e.g.plugins/state_delta/diff.tn) and constructors across the 69-plugin catalog continue compiling unchanged while preserving exact 64-bit integer fidelity (i64::MIN..i64::MAX). - Add
as_int(exacti64alias foras_num) and full type predicates (is_int,is_float,is_bool,is_str,is_arr,is_obj).
- Keep
Concise
json.libBuilder & Chaining API- Add
JsonEntry, scalar/container constructors (null_val,bool_val,int_val,float_val,str_val,arr,obj_empty,obj), key-value entry helpers (kv,kv_str,kv_int,kv_float,kv_bool,kv_null,kv_arr,kv_opt,kv_opt_str,kv_opt_int,kv_opt_bool), and immutable chaining helpers (with_kv,with_str,with_int,with_float,with_bool,with_opt,push_val).
- Add
Extend
codec.json(GEN_ABI 2) for Wire Strings andjson.lib.Value- Extend
derive Jsonto also emit<type>_to_json_str(v: T) -> strand<type>_from_json_str(text: str) -> result<T, JsonError>alongside<type>_to_json/<type>_from_json, keeping 100% compatibility with existing callers that do not depend onjson. - Add
derive JsonValue(codec.json.JsonValue) emitting<type>_to_value(v: T) -> Valueand<type>_from_value(v: Value) -> result<T, JsonError>for structs, enums (unit and payload variants), and newtypes, including nestedvec,option,map<str, T>,bytes(hex), and cross-module derived types. - Following the
GEN_ABI 2sandbox pattern established byapp_actions_gen(ADR-0293), modules usingderive JsonValueimportjson.lib { Value }; all generated helper aliases use a disjoint__codec_val_<type>_*prefix so a type can derive bothJsonandJsonValuein the same module. - Add integer range-validation helpers
(
value_to_i32,value_to_u8,value_to_u32,value_to_u64) tocodec.rtsofrom_valuefails closed on out-of-range integers, wrong JSON types, floats passed for integer fields, missing fields, unknown fields, and malformed enum variants.
- Extend
Consequences
- Zero Rust compiler changes (adheres to ADR-0291: "Freeze Rust, Grow in Tenon").
- Identical behavior across native C and
tenon interpbackends. - Wire JSON produced by
stringify(<type>_to_value(x))matches<type>_to_json_str(x)byte-for-byte, with exacti64::MIN/i64::MAXround-trip fidelity. - Existing
derive Jsonconsumers remain unaffected; modules opting intoValueAST derivation addimport json.lib { Value }andderive JsonValue for T.