| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
jsonb is a binary format JSON representation inspired by PostgreSQL and CockroachDB. It provides a fast, lightweight and easy-to-use API for working with JSON data.
The jsonb encoding format is a tree-like structure. Each node contains a container header, a number of JEntry headers, and nested encoding values.
// JSON value
[false, 10, {"k":"v"}]
// JSONB encoding
0x80000003 array container header (3 JEntries)
0x30000000 false JEntry header (no encoding value)
0x20000002 number JEntry header (encoding value length 2)
0x5000000e container JEntry header (encoding value length 14)
0x500a number encoding value (10)
0x40000001 object container header (1 JEntry)
0x10000001 string key JEntry header (encoding value length 1)
0x10000001 string value JEntry header (encoding value length 1)
0x6b string encoding value ("k")
0x76 string encoding value ("v")
The jsonb value is an enumeration that represents all kinds of JSON values and serves as an intermediate for converting other data types to the jsonb binary format value.
// jsonb value
#[derive(Clone, PartialEq, Eq)]
pub enum Value<'a> {
Null,
Bool(bool),
String(Cow<'a, str>),
Number(Number),
Array(Vec<Value<'a>>),
Object(Object<'a>),
}jsonb implements a number of commonly used built-in functions. Since most functions only focus on a subset of the total value, using container headers and JEntry headers to can efficiently skip over intermediate parts of the jsonb value. This avoids time-consuming deserialisation operations and provides very high performance. For more information, see https://docs.rs/jsonb/latest/jsonb/#functions
SQL/JSONPath is a query language used to select and extract a subset of elements from a jsonb value.
The following operators have been implemented:
| Operator | Description | Examples |
|---|---|---|
| $ | The root element | $ |
| @ | The current element in the filter expression | $.event?(@ == 1) |
| .* | Selecting all elements in an Object | $.* |
| .<name> | Selecting element that match the name in an Object | $.event |
| :<name> | Alias of .<name> | $:event |
| ["<name>"] | Alias of .<name> | $["event"] |
| [*] | Selecting all elements in an Array | $[*] |
| [<pos>, ..] | Selecting 0-based n-th elements in an Array | $[1, 2] |
| [last - <pos>, ..] | Selecting n-th element before the last element in an Array | $[0, last - 1] |
| [<pos1> to <pos2>, ..] | Selecting all elements of a range in an Array | $[1 to last - 2] |
| ?(<expr>) | Selecting all elements that matched the filter expression | $?(@.price < 10) |
fn main() {
let json = r#"
{
"name":"Fred",
"phones":[
{
"type":"home",
"number":3720453
},
{
"type": "work",
"number":5062051
}
]
}"#;
let path = r#"$.phones[*]?(@.number == 3720453)"#;
// parse JSON string to jsonb value
let value = jsonb::parse_value(json.as_bytes()).unwrap();
// encode jsonb value to jsonb binary value
let jsonb = value.to_vec();
// parse JSONPath string
let json_path = jsonb::jsonpath::parse_json_path(path.as_bytes()).unwrap();
// select subset value from jsonb binary value
let mut sub_jsonb = Vec::new();
let mut sub_offsets = Vec::new();
jsonb::get_by_path(&jsonb, json_path, &mut sub_jsonb, &mut sub_offsets);
// value={"number":3720453,"type":"home"}
println!("value={}", jsonb::to_string(&sub_jsonb));
}jsonb is an open source project and all kinds of contributions are welcome! You can help with ideas, code or documentation.
Licensed under the Apache License, Version 2.0
| Back | FazBrowse Home | New Git URL |