Expand description
General-purpose JSON codec for the SIM runtime.
This crate is a first-class, general-purpose codec: it round-trips every
kernel Expr losslessly by projecting the shared Expr graph onto
serde_json::Value using $expr-tagged forms (and $located forms when
source origins are carried). On top of that canonical projection it also
exposes interop surfaces aimed at foreign JSON consumers: a lossy untagged
projection mode and a small JSON Schema view for describing shapes.
The public surface is the JsonCodec runtime object (registered via
JsonCodecLib) plus the free conversion functions: the canonical
expr_to_json / json_to_expr and located/tree variants, the
mode-aware projection in project_expr_to_json / project_json_to_expr
(JsonProjectionMode), and the schema lowering in
shape_to_json_schema (ShapeSchema).
§Examples
Register the codec and round-trip s-expression-free text through the shared
Expr graph – decode JSON into an Expr, then encode it back:
use std::sync::Arc;
use sim_codec::{Input, decode_with_codec, encode_with_codec};
use sim_codec_json::JsonCodecLib;
use sim_kernel::{Cx, DefaultFactory, EagerPolicy, Expr, ReadPolicy, Symbol};
let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
sim_test_support::register_core_classes(&mut cx);
let lib = JsonCodecLib::new(cx.registry_mut().fresh_codec_id());
cx.load_lib(&lib)?;
let json = Symbol::qualified("codec", "json");
let expr = decode_with_codec(
&mut cx,
&json,
Input::Text(r#"{"$expr":"bool","value":true}"#.to_owned()),
ReadPolicy::default(),
)?;
assert_eq!(expr, Expr::Bool(true));
let text = encode_with_codec(&mut cx, &json, &expr, Default::default())?
.into_text()
.unwrap();
assert_eq!(text, r#"{"$expr":"bool","value":true}"#);The canonical projection functions can be used directly, without a runtime
context, for a pure Expr <-> JSON round-trip:
use sim_codec::{DecodeBudget, DecodeLimits};
use sim_codec_json::{expr_to_json, json_to_expr};
use sim_kernel::{CodecId, Expr};
let expr = Expr::Bytes(vec![0xfb, 0xef]);
let value = expr_to_json(&expr);
assert_eq!(value["base64"], serde_json::json!("++8="));
let mut budget = DecodeBudget::new(DecodeLimits::default());
let back = json_to_expr(CodecId(1), &value, &mut budget, 0)?;
assert_eq!(back, expr);Structs§
- Json
Codec - JSON codec runtime object that round-trips every
Exprthrough JSON. - Json
Codec Lib Libthat registers the JSON codec with the runtime.
Enums§
- Json
Projection Mode - Selects which
Expr <-> JSONprojection to apply. - Shape
Schema - A minimal, closed schema vocabulary that lowers to standard JSON Schema.
Statics§
- RECIPES
- Cookbook recipes for the JSON codec, embedded at build time from the crate’s
recipes/directory and exposed for help and browse surfaces.
Functions§
- expr_
to_ json - Projects an
Expronto a canonical$expr-taggedserde_json::Value. - json_
number_ to_ u64 - Reads a JSON number as
u64, accepting an unsigned literal directly or a non-negative signed literal. This is the one home for the provider token-count parser that the OpenAI server, chat codec, and HTTP runner each re-grew. - json_
to_ expr - Reads a canonical
$expr-taggedserde_json::Valueback into anExpr. - json_
to_ located_ expr - Reads JSON back into a
LocatedExpr, recovering the source origin from a$locatedwrapper when present and otherwise decoding a bare expression. - json_
to_ tree - Reads JSON back into a
LocatedExprTree, recovering per-node origins from$locatedwrappers, under the decodebudgetand nodedepthlimits. - located_
expr_ to_ json - Projects a
LocatedExpronto JSON, optionally wrapping it in a$locatedform that carries its source origin wheninclude_originis set. - project_
expr_ to_ json - Projects an
Exprto aserde_json::Valueusing the given mode. - project_
json_ to_ expr - Projects a
serde_json::Valueto anExprusing the given mode. - project_
json_ to_ expr_ budgeted - Budget-aware variant of
project_json_to_expr. - shape_
to_ json_ schema - Lowers a
ShapeSchemato a standard JSON Schema document. - tree_
to_ json - Projects a
LocatedExprTreeonto JSON, recursively carrying per-node origins as$locatedforms wheninclude_originis set.