Expand description
JSON representation.
This module provides a serde_json-like API that is backed by the native
Materialize data format, i.e., Row and Datum. It supports
efficiently parsing and serializing JSON strings and byte slices with
minimal allocations. It also provides seamless interop with APIs that
require serde_json::Value, though at a small performance cost.
There are two core types in the module:
-
Jsonbrepresents owned JSON data. This type houses the deserialization functions. -
JsonbRefis a borrowed view of JSON data. This type houses the serialization functions.
The name “jsonb” is based on the PostgreSQL data type of the same name.
Various sources claim this stands for “JSON better”, as compared to the
less-efficient json data type in PostgreSQL.
§Constructing JSON objects
To parse JSON from a string, use the FromStr implementation. Once
parsed, the underlying Row can be extracted with Jsonb::into_row.
let jsonb: Jsonb = r#"{"a": 1, "b": 2}"#.parse()?;
let row = jsonb.into_row();If the source JSON is in bytes, use Jsonb::from_slice instead:
let jsonb = Jsonb::from_slice(br#"{"a": 1, "b": 2}"#);§Serializing JSON objects
To write a JSON object to a string, use the fmt::Display implementation.
The alternate format produces pretty output.
format!("compressed: {}", jsonb);
format!("pretty: {:#}", jsonb);§Direct JSON deserialization
You can skip Jsonb entirely and deserialize JSON directly into an
existing Row with JsonbPacker. This saves an allocation and a
copy.
let mut row = Row::default();
let mut packer = row.packer();
packer.push(Datum::Int32(42));
JsonbPacker::new(&mut packer).pack_str("[1, 2]")?;Modules§
Structs§
- Collector 🔒
- Dict
Entry 🔒 - Jsonb
- An owned JSON value backed by a
Row. - Jsonb
Datum 🔒 - A wrapper for
Datumthat implementsSerialize. - Jsonb
Packer - A JSON deserializer that decodes directly into an existing
RowPacker. - Jsonb
Ref - A borrowed JSON value.
- KeyClassifier 🔒
- Number
Parser 🔒 - Writer
Formatter 🔒 - Implements
io::Writeforfmt::Formatter.
Enums§
Constants§
Functions§
- extract_
value 🔒 - Extracts a self-contained slice of commands for the next parse node.
- pack 🔒
- pack_
dict 🔒 - Packs a sequence of (key, val) pairs as an ordered dictionary.
- pack_
list 🔒 - Packs a sequence of values as an ordered list.
- pack_
value 🔒