pub struct PgJsonb { /* private fields */ }Expand description
A PostgreSQL jsonb value, its object keys in jsonb’s storage order with the last of
duplicate keys kept, equal, ordered and hashed as jsonb’s btree operator class compares it.
The order puts an empty top-level array first, then null, strings, numbers, booleans,
arrays and objects. Arrays compare by element count and then element by element, objects by
pair count and then key and value in storage order, and numbers by value, so 1.0 equals
1. Strings and keys compare by their bytes, as under the C collation. A server whose
default collation is another one orders strings by it, while equality stays the same.
Its Display is jsonb_out, so equal values can print differently.
use sql_values::postgres::PgJsonb;
let jsonb = |text| PgJsonb::parse(text).unwrap();
assert_eq!(jsonb(r#"{"bb":1,"a":2,"c":3,"a":4}"#).to_string(), r#"{"a": 4, "c": 3, "bb": 1}"#);
assert_eq!(jsonb(" [ 1 , {\"b\" : null } ] ").to_string(), r#"[1, {"b": null}]"#);
assert_eq!(jsonb("1.0"), jsonb("1"));
assert_eq!(jsonb("1.0").to_string(), "1.0");
assert!(jsonb("[]") < jsonb("null"));
assert!(jsonb(r#""B""#) < jsonb(r#""a""#));
assert!(jsonb("true") < jsonb("[null]"));
assert!(jsonb(r#"{"b": 1}"#) < jsonb(r#"{"a": 1, "b": 2}"#));Implementations§
Source§impl PgJsonb
impl PgJsonb
Sourcepub fn parse(text: &str) -> Result<Self, JsonbError>
pub fn parse(text: &str) -> Result<Self, JsonbError>
PostgreSQL’s jsonb_in, reading one JSON value between blanks, its numbers read by
numeric_in and its \u escapes decoded with surrogate pairs combined.
PostgreSQL also refuses nesting deeper than its max_stack_depth allows and strings or
containers beyond the 256 MiB its binary format holds. Those limits depend on the
server, and this reads any depth and size.
§Errors
The JsonbError PostgreSQL raises, the first one in the text as it reads it.
use sql_values::postgres::{JsonbError, PgJsonb};
let printed = |text| PgJsonb::parse(text).map(|jsonb| jsonb.to_string());
assert_eq!(printed("1e2").unwrap(), "100");
assert_eq!(printed("-0.0").unwrap(), "0.0");
assert_eq!(printed(r#""\u00e9 \ud83d\ude00 \/""#).unwrap(), "\"é 😀 /\"");
assert_eq!(printed("[1 2]"), Err(JsonbError::ExpectedCommaOrArrayEnd));
assert_eq!(printed("[1,"), Err(JsonbError::UnexpectedEnd));Sourcepub fn to_text(&self) -> Option<Cow<'_, str>>
pub fn to_text(&self) -> Option<Cow<'_, str>>
The text #>> '{}' extracts, the one ->> and #>> give for a value they reach, and
None for null. A string gives its contents as they are, without the quotes and
escapes Display adds, and any other value gives what jsonb_out prints.
use sql_values::postgres::PgJsonb;
let text = |json| PgJsonb::parse(json).unwrap().to_text().map(String::from);
assert_eq!(text(r#""a\nb \" \u00e9""#).as_deref(), Some("a\nb \" é"));
assert_eq!(text(r#""8""#).as_deref(), Some("8"));
assert_eq!(text("1e2").as_deref(), Some("100"));
assert_eq!(text("1.50").as_deref(), Some("1.50"));
assert_eq!(text("true").as_deref(), Some("true"));
assert_eq!(text("null"), None);
assert_eq!(text(r#"{"b":1,"a":[1, 2]}"#).as_deref(), Some(r#"{"a": [1, 2], "b": 1}"#));
assert_eq!(text(r#"["a"]"#).as_deref(), Some(r#"["a"]"#));Trait Implementations§
Source§impl Display for PgJsonb
JsonbToCString, keys and values joined by ": ", pairs and elements by ", ".
impl Display for PgJsonb
JsonbToCString, keys and values joined by ": ", pairs and elements by ", ".
impl Eq for PgJsonb
Source§impl Ord for PgJsonb
compareJsonbContainers, both token streams walked together to the first difference. A
top-level scalar is a one-element pseudo array there, so it sorts after the empty array and
before every other array and object.
impl Ord for PgJsonb
compareJsonbContainers, both token streams walked together to the first difference. A
top-level scalar is a one-element pseudo array there, so it sorts after the empty array and
before every other array and object.