Skip to main content

Crate sim_codec_json

Crate sim_codec_json 

Source
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§

JsonCodec
JSON codec runtime object that round-trips every Expr through JSON.
JsonCodecLib
Lib that registers the JSON codec with the runtime.

Enums§

JsonProjectionMode
Selects which Expr <-> JSON projection to apply.
ShapeSchema
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 Expr onto a canonical $expr-tagged serde_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-tagged serde_json::Value back into an Expr.
json_to_located_expr
Reads JSON back into a LocatedExpr, recovering the source origin from a $located wrapper when present and otherwise decoding a bare expression.
json_to_tree
Reads JSON back into a LocatedExprTree, recovering per-node origins from $located wrappers, under the decode budget and node depth limits.
located_expr_to_json
Projects a LocatedExpr onto JSON, optionally wrapping it in a $located form that carries its source origin when include_origin is set.
project_expr_to_json
Projects an Expr to a serde_json::Value using the given mode.
project_json_to_expr
Projects a serde_json::Value to an Expr using the given mode.
project_json_to_expr_budgeted
Budget-aware variant of project_json_to_expr.
shape_to_json_schema
Lowers a ShapeSchema to a standard JSON Schema document.
tree_to_json
Projects a LocatedExprTree onto JSON, recursively carrying per-node origins as $located forms when include_origin is set.