pub struct JsonCell(/* private fields */);Expand description
One cell of a query result row, carried as the JSON text the server sent.
serde_json::Value has no arbitrary-precision number: a JSON number that
does not fit an i64/u64 is parsed through f64, so a DECIMAL(38,2)
carrying 22 significant digits loses everything past the 17th before a
caller ever sees it. Holding the token text decides nothing at parse time —
the digits the server wrote are the digits that come back out.
Serializing re-emits that text verbatim, so a cell round-trips to the server’s own bytes: a number stays an unquoted JSON number rather than turning into a string.
serde_json is the only supported format. The underlying RawValue
captures and re-emits text through a private newtype that other serde
formats do not recognise, so deserializing a cell from YAML fails and
serializing one to YAML writes the JSON text as a string. Go through
to_value when a cell has to reach a non-JSON format,
accepting the rounding that costs.
Implementations§
Source§impl JsonCell
impl JsonCell
Sourcepub fn from_json_text(text: String) -> Result<Self, Error>
pub fn from_json_text(text: String) -> Result<Self, Error>
Adopt text — one complete JSON value — as a cell.
Errors when text is not a single well-formed JSON value. The text is
stored as given and re-emitted byte for byte on serialize.
Sourcepub fn as_json_str(&self) -> &str
pub fn as_json_str(&self) -> &str
The cell’s JSON text, exactly as it arrived.
This is the lossless view: for a number it is every digit the server wrote, whatever its width.
Sourcepub fn kind(&self) -> JsonCellKind
pub fn kind(&self) -> JsonCellKind
Which JSON type this cell holds.
Read off the first byte, which a well-formed JSON value always has and which is enough to tell the six types apart.
Sourcepub fn as_str(&self) -> Option<Cow<'_, str>>
pub fn as_str(&self) -> Option<Cow<'_, str>>
The contents of a JSON string cell, unescaped. None for every other
kind — including a number, whose text is available from
as_json_str.
Borrows out of the stored text for a string with no escape sequence,
which is the common case; only an escaped string is decoded into an
owned String. Reading one string column down a large result
therefore allocates per escaped value rather than per row.
Sourcepub fn as_array(&self) -> Option<Vec<JsonCell>>
pub fn as_array(&self) -> Option<Vec<JsonCell>>
The elements of a JSON array cell, each still carried as text. None
for every other kind.
Elements keep their own digits: descending into an array does not go through a value tree.
Sourcepub fn to_value(&self) -> Value
pub fn to_value(&self) -> Value
Parse the cell into a value tree.
Rounds a number too wide for an f64 — the loss this type exists to
avoid — so prefer as_json_str wherever the text
will do, and reach for this only when a caller genuinely needs
serde_json::Value.
§Panics
If the stored text is not valid JSON, which a cell cannot hold: every
constructor goes through RawValue, which validates. A failure here is
a broken invariant, not a bad cell. It panics rather than substituting
Value::Null, which would be indistinguishable from a cell that really
is null.