Skip to main content

Module json

Module json 

Source
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::Serialize wrapper that streams a stmt::Value as JSON.
Seed
A type-directed serde::de::DeserializeSeed that decodes JSON straight into a stmt::Value of the seed’s stmt::Type — no serde_json::Value intermediate. The type is required because the wire form is ambiguous for scalars (Uuid/String are both strings; the integer widths are all numbers). A structural Type::Object position switches to shape-directed decoding ([AnySeed]).

Functions§

from_slice
Decode a JSON document (UTF-8 bytes) into a stmt::Value of type ty.
from_str
Decode a JSON document (string) into a stmt::Value of type ty.
list_from_slice
Decode a JSON array (UTF-8 bytes) into a Value::List, using elem_ty as the per-element type.
list_from_str
Decode a JSON array (string) into a Value::List, using elem_ty as the per-element type.
to_string
Encode a stmt::Value as a JSON string.
to_vec
Encode a stmt::Value as JSON UTF-8 bytes.