baml.FromJson
JSON deserialization with an implicit structural default.
Reference version
Signature
interface baml.FromJsonJSON deserialization with an implicit structural default.
A type implements baml.FromJson to control how baml.json.to<T>(j) (and
JSON deserialization generally) constructs it from a json value. Unlike
to_json, from_json has no self: it is an associated constructor taking
a json and returning Self. Implicit implementations decode
structurally, field by field, honoring nested baml.FromJson overrides, so
implement this only when a type's json shape differs from its field layout.
class Temp {
celsius float
implements baml.FromJson {
function from_json(j: baml.json.json) -> Self throws baml.json.DecodeError {
Temp { celsius: baml.json.to<float>(baml.json.field(j, "c")) }
}
}
}
from_json is required in an explicit implementation. The compiler supplies
a structural implementation when no explicit implementation applies.
Source:<builtin>/baml/conversions.bamlbytes 7519–7621