baml.json.from
Converts any BAML value to a `json` value.
Signature
Converts any BAML value to a json value.
The conversion follows the value's runtime type. Primitives, lists, and
maps take their natural JSON shape. A class instance becomes an object of
its fields, an enum value its variant's name, and media the tagged object
{ kind, source, value, mime, name? }. A bigint becomes an
arbitrary-precision JSON number. A float that is NaN or infinite stays
as it is: a json value holds it, but stringify refuses to write it.
A value whose runtime class implements baml.ToJson with its own
to_json is converted by that method instead, at any depth, and the
method owns its whole subtree: naming and omit_nulls do not reach
inside it. The method is found from the value's runtime class, so one
declared in any package is honored.
naming picks the names fields and variants go by. The default,
Naming.Declared, writes every field under its declared name.
Naming.Aliased writes a field or variant under its @alias, and leaves
out a @skip field.
omit_nulls leaves out a class field that holds null, so an unset
optional field writes {"name": "Ada"} rather than
{"name": "Ada", "nickname": null}. Nothing else is left out: a null
map value, a null list element, and a null nested in a json value
stay, because there null is data.
Throws
baml.json.SerializationErrorif any value in the graph has no JSON form: auint8array(encode it explicitly withto_base64()orto_hex()), a function, a future, a map whose keys are not strings, a value that contains itself, or any other opaque runtime object.reasonnames which.baml.json.SerializationErrorifnamingisNaming.Aliasedand the value holds a@skipenum variant, which then has no name.
Source:<builtin>/baml/ns_json/json.bamlbytes 9869–10058