baml.json.to_json

Serializes any BAML value to its `json` representation, dispatching on the value's runtime type.

Reference version

Signature

function baml.json.to_json(value: unknown) -> baml.json.json throws baml.json.SerializationError

Serializes any BAML value to its json representation, dispatching on the value's runtime type.

The untyped spelling of from<T> — same walk, same baml.ToJson override handling at every depth, same output. Only the signature differs: unknown in place of a type parameter, which makes it callable where no static type is available to instantiate from<T> with, and callable by name from a host.

Prefer from<T> in BAML code. It is what a bare v.to_json() desugars to, and it keeps the argument's type at the call site instead of erasing it to unknown. This one exists so to_string and engine-side callers have a non-generic symbol to bind.

Throws

  • baml.json.SerializationError if any value in the graph has no JSON form. See from<T> for the rendering rules.

Source:<builtin>/baml/ns_json/json.bamlbytes 4530–4619