Expand description
JSON encoding for stmt::Values stored in document-backed columns.
JSON encoding for stmt::Values stored in document-backed columns
(MySQL JSON, SQLite TEXT via the JSON1 extension, and PostgreSQL jsonb
for #[document]-marked fields).
The conversion does not go through an intermediate serde_json::Value
tree. Encoding streams a stmt::Value straight to JSON text via a
serde::Serialize wrapper ([Encode]); decoding parses JSON tokens
straight into a correctly-typed stmt::Value via a type-directed
serde::de::DeserializeSeed ([Seed]).
The serde impls live on local wrappers, not on stmt::Value itself. The
encoding is opinionated (UUIDs / decimals / timestamps as JSON strings, to
match the per-column TEXT encoding the same scalar has at the SQL level) and
backends with typed document storage (BSON, DynamoDB) need different
representations — so stmt::Value is deliberately left without a canonical
serde representation. Decoding is type-directed for scalars: Value::Uuid
vs Value::String are both JSON strings on the wire, and Value::I64 vs
Value::U64 are both JSON numbers; only the caller’s stmt::Type
distinguishes them, which is why decode carries the type as a seed rather
than a plain Deserialize.
A #[document] column is typed by the structural stmt::Type::Object and
decodes shape-directed: a JSON object becomes a named Value::Object in
wire key order, and its interior leaves take their wire shapes (strings
stay strings, numbers decode by integer fit). The query engine — the only
party that knows which embedded model the column stores — raises the wire
object into a typed positional record; no schema is consulted here.
Structs§
- Encode
- A
serde::Serializewrapper that streams astmt::Valueas JSON. - Seed
- A type-directed
serde::de::DeserializeSeedthat decodes JSON straight into astmt::Valueof the seed’sstmt::Type— noserde_json::Valueintermediate. The type is required because the wire form is ambiguous for scalars (Uuid/Stringare both strings; the integer widths are all numbers). A structuralType::Objectposition switches to shape-directed decoding ([AnySeed]).
Functions§
- from_
slice - Decode a JSON document (UTF-8 bytes) into a
stmt::Valueof typety. - from_
str - Decode a JSON document (string) into a
stmt::Valueof typety. - list_
from_ slice - Decode a JSON array (UTF-8 bytes) into a
Value::List, usingelem_tyas the per-element type. - list_
from_ str - Decode a JSON array (string) into a
Value::List, usingelem_tyas the per-element type. - to_
string - Encode a
stmt::Valueas a JSON string. - to_vec
- Encode a
stmt::Valueas JSON UTF-8 bytes.