pub struct RawJsonb<'a> { /* private fields */ }Expand description
Represents JSONB data wrapped around a raw, immutable slice of bytes.
It does not own the underlying data, allowing various operations to be performed on the JSONB data without copying.
This is critical for performance when dealing with large JSONB values.
RawJsonb provides various methods to inspect and manipulate the JSONB data efficiently.
Implementations§
Source§impl RawJsonb<'_>
impl RawJsonb<'_>
Sourcepub fn get_by_index(&self, index: usize) -> Result<Option<OwnedJsonb>, Error>
pub fn get_by_index(&self, index: usize) -> Result<Option<OwnedJsonb>, Error>
Gets the element at the specified index in a JSONB array.
If the JSONB value is an array, this function returns the element at the given index as an OwnedJsonb.
If the index is out of bounds, it returns Ok(None).
If the JSONB value is not an array (e.g., it’s an object or a scalar), this function also returns Ok(None).
§Arguments
self- The JSONB value.index- The index of the desired element.
§Returns
Ok(Some(OwnedJsonb))- The element at the specified index as anOwnedJsonbif the input is an array and the index is valid.Ok(None)- If the input is not an array, or if the index is out of bounds.Err(Error)- If an error occurred during decoding (e.g., invalid JSONB data).
§Examples
use lance_jsonb::OwnedJsonb;
let arr_jsonb = r#"[1, "hello", {"a": 1}]"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = arr_jsonb.as_raw();
let element0 = raw_jsonb.get_by_index(0).unwrap();
assert_eq!(element0.unwrap().to_string(), "1");
let element1 = raw_jsonb.get_by_index(1).unwrap();
assert_eq!(element1.unwrap().to_string(), r#""hello""#);
let element2 = raw_jsonb.get_by_index(2).unwrap();
assert_eq!(element2.unwrap().to_string(), r#"{"a":1}"#);
let element3 = raw_jsonb.get_by_index(3).unwrap();
assert!(element3.is_none()); // Index out of bounds
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = obj_jsonb.as_raw();
let element = raw_jsonb.get_by_index(0).unwrap();
assert!(element.is_none()); // Not an arraySourcepub fn get_by_name(
&self,
name: &str,
ignore_case: bool,
) -> Result<Option<OwnedJsonb>, Error>
pub fn get_by_name( &self, name: &str, ignore_case: bool, ) -> Result<Option<OwnedJsonb>, Error>
Gets the value associated with a given key in a JSONB object.
If the JSONB value is an object, this function searches for a key matching the provided name
and returns the associated value as an OwnedJsonb.
The ignore_case parameter controls whether the key search is case-sensitive.
If the key is not found, it returns Ok(None).
If the JSONB value is not an object (e.g., it’s an array or a scalar), this function also returns Ok(None).
§Arguments
self- The JSONB value.name- The key to search for.ignore_case- Whether the key search should be case-insensitive.
§Returns
Ok(Some(OwnedJsonb))- The value associated with the key as anOwnedJsonb, if the input is an object and the key is found.Ok(None)- If the input is not an object, or if the key is not found.Err(Error)- If an error occurred during decoding (e.g., invalid JSONB data).
§Examples
use lance_jsonb::OwnedJsonb;
let obj_jsonb = r#"{"a": 1, "b": "hello", "c": [1, 2]}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = obj_jsonb.as_raw();
let value_a = raw_jsonb.get_by_name("a", false).unwrap();
assert_eq!(value_a.unwrap().to_string(), "1");
let value_b = raw_jsonb.get_by_name("b", false).unwrap();
assert_eq!(value_b.unwrap().to_string(), r#""hello""#);
let value_c = raw_jsonb.get_by_name("c", false).unwrap();
assert_eq!(value_c.unwrap().to_string(), "[1,2]");
let value_d = raw_jsonb.get_by_name("d", false).unwrap();
assert!(value_d.is_none()); // Key not found
// Case-insensitive search
let value_a_case_insensitive = raw_jsonb.get_by_name("A", true).unwrap();
assert_eq!(value_a_case_insensitive.unwrap().to_string(), "1");
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_jsonb = arr_jsonb.as_raw();
let value = raw_jsonb.get_by_name("a", false).unwrap();
assert!(value.is_none()); // Not an objectSource§impl RawJsonb<'_>
impl RawJsonb<'_>
Sourcepub fn is_null(&self) -> Result<bool, Error>
pub fn is_null(&self) -> Result<bool, Error>
Checks if the JSONB value is null.
This function determines whether the JSONB value represents a JSON null.
§Arguments
self- The JSONB value.
§Returns
Ok(true)if the value is null.Ok(false)if the value is not null.Err(Error)- If the input JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_jsonb = null_jsonb.as_raw();
assert!(raw_jsonb.is_null().unwrap());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = obj_jsonb.as_raw();
assert!(!raw_jsonb.is_null().unwrap());Sourcepub fn is_boolean(&self) -> Result<bool, Error>
pub fn is_boolean(&self) -> Result<bool, Error>
Checks if the JSONB value is a boolean.
This function checks if the JSONB value represents a JSON boolean (true or false).
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is a boolean (trueorfalse).Ok(false)- If the value is not a boolean.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Boolean values
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_true = true_jsonb.as_raw();
assert!(raw_true.is_boolean().unwrap());
let false_jsonb = "false".parse::<OwnedJsonb>().unwrap();
let raw_false = false_jsonb.as_raw();
assert!(raw_false.is_boolean().unwrap());
// Non-boolean values
let num_jsonb = "1".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(!raw_num.is_boolean().unwrap());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert!(!raw_arr.is_boolean().unwrap());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert!(!raw_obj.is_boolean().unwrap());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert!(!raw_null.is_boolean().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_boolean();
assert!(result.is_err());Sourcepub fn to_bool(&self) -> Result<bool, Error>
pub fn to_bool(&self) -> Result<bool, Error>
Converts a JSONB value to a boolean.
This function attempts to convert a JSONB value to a boolean. It prioritizes extracting a boolean value directly if possible. If the value is a string, it converts the string to lowercase and checks if it’s “true” or “false”. Otherwise, it returns an error.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is JSONtrueor a string that is “true” (case-insensitive).Ok(false)- If the value is JSONfalseor a string that is “false” (case-insensitive).Err(Error::InvalidCast)- If the value cannot be converted to a boolean.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Boolean values
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
assert!(true_jsonb.as_raw().to_bool().unwrap());
let false_jsonb = "false".parse::<OwnedJsonb>().unwrap();
assert!(!false_jsonb.as_raw().to_bool().unwrap());
// String representations of booleans
let true_str = r#""true""#.parse::<OwnedJsonb>().unwrap();
assert!(true_str.as_raw().to_bool().unwrap());
let false_str = r#""false""#.parse::<OwnedJsonb>().unwrap();
assert!(!false_str.as_raw().to_bool().unwrap());
let true_str_lowercase = r#""TRUE""#.parse::<OwnedJsonb>().unwrap();
assert!(true_str_lowercase.as_raw().to_bool().unwrap());
// Invalid conversions
let num_jsonb = "1".parse::<OwnedJsonb>().unwrap();
let result = num_jsonb.as_raw().to_bool();
assert!(result.is_err());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let result = arr_jsonb.as_raw().to_bool();
assert!(result.is_err());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let result = obj_jsonb.as_raw().to_bool();
assert!(result.is_err());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let result = null_jsonb.as_raw().to_bool();
assert!(result.is_err());
let invalid_str = r#""maybe""#.parse::<OwnedJsonb>().unwrap();
let result = invalid_str.as_raw().to_bool();
assert!(result.is_err());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.to_bool();
assert!(result.is_err());Sourcepub fn is_number(&self) -> Result<bool, Error>
pub fn is_number(&self) -> Result<bool, Error>
Checks if the JSONB value is a number.
This function checks if the JSONB value represents a JSON number.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is a number.Ok(false)- If the value is not a number.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Number values
let num_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(raw_num.is_number().unwrap());
let num_jsonb = "123".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(raw_num.is_number().unwrap());
let num_jsonb = "-123.45".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(raw_num.is_number().unwrap());
// Non-number values
let bool_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_bool = bool_jsonb.as_raw();
assert!(!raw_bool.is_number().unwrap());
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert!(!raw_str.is_number().unwrap());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert!(!raw_arr.is_number().unwrap());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert!(!raw_obj.is_number().unwrap());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert!(!raw_null.is_number().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_number();
assert!(result.is_err());Sourcepub fn as_number(&self) -> Result<Option<Number>, Error>
pub fn as_number(&self) -> Result<Option<Number>, Error>
Extracts a number from a JSONB value.
This function attempts to extract a number from the JSONB value.
If the JSONB value is a number, it returns the number; otherwise, it returns None.
§Arguments
self- The JSONB value.
§Returns
Ok(Some(Number))- If the value is a number, the extracted number.Ok(None)- If the value is not a number.Err(Error)- If the JSONB data is invalid or if the number cannot be decoded.
§Examples
use lance_jsonb::Number;
use lance_jsonb::OwnedJsonb;
use lance_jsonb::RawJsonb;
// Number value
let num_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert_eq!(raw_num.as_number().unwrap(), Some(Number::Float64(123.45)));
let num_jsonb = "-123".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert_eq!(raw_num.as_number().unwrap(), Some(Number::Int64(-123)));
// Non-number values
let bool_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_bool = bool_jsonb.as_raw();
assert_eq!(raw_bool.as_number().unwrap(), None);
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert_eq!(raw_str.as_number().unwrap(), None);
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert_eq!(raw_arr.as_number().unwrap(), None);
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert_eq!(raw_obj.as_number().unwrap(), None);
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert_eq!(raw_null.as_number().unwrap(), None);
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.as_number();
assert!(result.is_err());
// Invalid Number (corrupted data)
let corrupted_num_jsonb = OwnedJsonb::from(vec![10, 0, 0, 0, 16, 0, 0, 0, 0, 1]);
let corrupted_raw_num_jsonb = corrupted_num_jsonb.as_raw();
let result = corrupted_raw_num_jsonb.as_number();
assert!(result.is_err()); // Decodes should return ErrSourcepub fn is_i64(&self) -> Result<bool, Error>
pub fn is_i64(&self) -> Result<bool, Error>
Checks whether the JSONB value is an exact i64.
Decimal and floating-point numbers must already be integral.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is an exacti64.Ok(false)- Otherwise.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// i64 values
let i64_jsonb = "123456789012345678".parse::<OwnedJsonb>().unwrap();
let raw_i64 = i64_jsonb.as_raw();
assert!(raw_i64.is_i64().unwrap());
let i64_jsonb = "-123456789012345678.0".parse::<OwnedJsonb>().unwrap();
let raw_i64 = i64_jsonb.as_raw();
assert!(raw_i64.is_i64().unwrap());
let float_jsonb = "1.5e0".parse::<OwnedJsonb>().unwrap();
let raw_float = float_jsonb.as_raw();
assert!(!raw_float.is_i64().unwrap());
// Out-of-range values
let float_jsonb = "1e100".parse::<OwnedJsonb>().unwrap();
let raw_float = float_jsonb.as_raw();
assert!(!raw_float.is_i64().unwrap());
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert!(!raw_str.is_i64().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_i64();
assert!(result.is_err());Sourcepub fn to_i64(&self) -> Result<i64, Error>
pub fn to_i64(&self) -> Result<i64, Error>
Converts a JSONB value to i64.
Numbers are rounded to the nearest integer. Booleans map to 1 and
0. Strings are parsed as i64, or as f64 and then rounded.
§Arguments
self- The JSONB value.
§Returns
Ok(i64)- The converted value.Err(Error::InvalidCast)- If the value cannot be converted to ani64.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Integer values
let i64_jsonb = "123".parse::<OwnedJsonb>().unwrap();
assert_eq!(i64_jsonb.as_raw().to_i64().unwrap(), 123);
let i64_jsonb = "-42".parse::<OwnedJsonb>().unwrap();
assert_eq!(i64_jsonb.as_raw().to_i64().unwrap(), -42);
// Boolean values
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
assert_eq!(true_jsonb.as_raw().to_i64().unwrap(), 1);
let false_jsonb = "false".parse::<OwnedJsonb>().unwrap();
assert_eq!(false_jsonb.as_raw().to_i64().unwrap(), 0);
// String representation of an integer
let str_jsonb = r#""123""#.parse::<OwnedJsonb>().unwrap();
assert_eq!(str_jsonb.as_raw().to_i64().unwrap(), 123);
// Decimal and float numbers are rounded before conversion
let decimal_jsonb = "123.5".parse::<OwnedJsonb>().unwrap();
assert_eq!(decimal_jsonb.as_raw().to_i64().unwrap(), 124);
let float_jsonb = "1.5e0".parse::<OwnedJsonb>().unwrap();
assert_eq!(float_jsonb.as_raw().to_i64().unwrap(), 2);
let str_jsonb = r#""1.5""#.parse::<OwnedJsonb>().unwrap();
assert_eq!(str_jsonb.as_raw().to_i64().unwrap(), 2);
// Invalid conversions
let float_jsonb = "1e100".parse::<OwnedJsonb>().unwrap();
let result = float_jsonb.as_raw().to_i64();
assert!(result.is_err());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let result = arr_jsonb.as_raw().to_i64();
assert!(result.is_err());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let result = obj_jsonb.as_raw().to_i64();
assert!(result.is_err());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let result = null_jsonb.as_raw().to_i64();
assert!(result.is_err());
let invalid_str_jsonb = r#""abc""#.parse::<OwnedJsonb>().unwrap();
let result = invalid_str_jsonb.as_raw().to_i64();
assert!(result.is_err());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.to_i64();
assert!(result.is_err());Sourcepub fn to_f64(&self) -> Result<f64, Error>
pub fn to_f64(&self) -> Result<f64, Error>
Converts a JSONB value to an f64 floating-point number.
This function attempts to convert a JSONB value to an f64 floating-point number.
It prioritizes direct conversion from a number if possible.
If the value is a boolean, it’s converted to 1.0 (for true) or 0.0 (for false).
If the value is a string that can be parsed as an f64, that parsed value is returned.
Otherwise, an error is returned.
§Arguments
self- The JSONB value.
§Returns
Ok(f64)- Thef64representation of the JSONB value.Err(Error::InvalidCast)- If the value cannot be converted to anf64(e.g., it’s an array, an object, a string that is not a valid number, or a null value).Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// f64 values
let f64_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
assert_eq!(f64_jsonb.as_raw().to_f64().unwrap(), 123.45);
let int_jsonb = "123".parse::<OwnedJsonb>().unwrap();
assert_eq!(int_jsonb.as_raw().to_f64().unwrap(), 123.0);
// Boolean values
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
assert_eq!(true_jsonb.as_raw().to_f64().unwrap(), 1.0);
let false_jsonb = "false".parse::<OwnedJsonb>().unwrap();
assert_eq!(false_jsonb.as_raw().to_f64().unwrap(), 0.0);
// String representation of a number
let str_jsonb = r#""123.45""#.parse::<OwnedJsonb>().unwrap();
assert_eq!(str_jsonb.as_raw().to_f64().unwrap(), 123.45);
// Invalid conversions
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let result = arr_jsonb.as_raw().to_f64();
assert!(result.is_err());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let result = obj_jsonb.as_raw().to_f64();
assert!(result.is_err());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let result = null_jsonb.as_raw().to_f64();
assert!(result.is_err());
let invalid_str_jsonb = r#""abc""#.parse::<OwnedJsonb>().unwrap();
let result = invalid_str_jsonb.as_raw().to_f64();
assert!(result.is_err());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.to_f64();
assert!(result.is_err());Sourcepub fn is_string(&self) -> Result<bool, Error>
pub fn is_string(&self) -> Result<bool, Error>
Checks if the JSONB value is a string.
This function checks if the JSONB value represents a JSON string.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is a string.Ok(false)- If the value is not a string.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// String value
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert!(raw_str.is_string().unwrap());
// Non-string values
let num_jsonb = "123".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(!raw_num.is_string().unwrap());
let bool_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_bool = bool_jsonb.as_raw();
assert!(!raw_bool.is_string().unwrap());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert!(!raw_arr.is_string().unwrap());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert!(!raw_obj.is_string().unwrap());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert!(!raw_null.is_string().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_string();
assert!(result.is_err());Sourcepub fn to_str(&self) -> Result<String, Error>
pub fn to_str(&self) -> Result<String, Error>
Converts a JSONB value to a String.
This function attempts to convert a JSONB value to a string representation. It prioritizes direct conversion from strings. Booleans are converted to “true” or “false”, and numbers are converted to their string representations. Other types (arrays, objects, null) will result in an error.
§Arguments
self- The JSONB value.
§Returns
Ok(String)- The string representation of the JSONB value.Err(Error::InvalidCast)- If the JSONB value cannot be converted to a string (e.g., it’s an array, an object, or a null value).Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// String value
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
assert_eq!(str_jsonb.as_raw().to_str().unwrap(), "hello");
// Number value
let num_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
assert_eq!(num_jsonb.as_raw().to_str().unwrap(), "123.45");
// Boolean values
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
assert_eq!(true_jsonb.as_raw().to_str().unwrap(), "true");
let false_jsonb = "false".parse::<OwnedJsonb>().unwrap();
assert_eq!(false_jsonb.as_raw().to_str().unwrap(), "false");
// Invalid conversions
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let result = arr_jsonb.as_raw().to_str();
assert!(result.is_err());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let result = obj_jsonb.as_raw().to_str();
assert!(result.is_err());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let result = null_jsonb.as_raw().to_str();
assert!(result.is_err());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.to_str();
assert!(result.is_err());Sourcepub fn is_array(&self) -> Result<bool, Error>
pub fn is_array(&self) -> Result<bool, Error>
Checks if the JSONB value is an array.
This function checks if the JSONB value represents a JSON array.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is an array.Ok(false)- If the value is not an array.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Array value
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert!(raw_arr.is_array().unwrap());
// Non-array values
let num_jsonb = "123".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(!raw_num.is_array().unwrap());
let bool_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_bool = bool_jsonb.as_raw();
assert!(!raw_bool.is_array().unwrap());
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert!(!raw_str.is_array().unwrap());
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert!(!raw_obj.is_array().unwrap());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert!(!raw_null.is_array().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_array();
assert!(result.is_err());Sourcepub fn is_object(&self) -> Result<bool, Error>
pub fn is_object(&self) -> Result<bool, Error>
Checks if the JSONB value is an object.
This function checks if the JSONB value represents a JSON object.
§Arguments
self- The JSONB value.
§Returns
Ok(true)- If the value is an object.Ok(false)- If the value is not an object.Err(Error)- If the JSONB data is invalid.
§Examples
use lance_jsonb::OwnedJsonb;
// Object value
let obj_jsonb = r#"{"a": 1}"#.parse::<OwnedJsonb>().unwrap();
let raw_obj = obj_jsonb.as_raw();
assert!(raw_obj.is_object().unwrap());
// Non-object values
let num_jsonb = "123".parse::<OwnedJsonb>().unwrap();
let raw_num = num_jsonb.as_raw();
assert!(!raw_num.is_object().unwrap());
let bool_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_bool = bool_jsonb.as_raw();
assert!(!raw_bool.is_object().unwrap());
let str_jsonb = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let raw_str = str_jsonb.as_raw();
assert!(!raw_str.is_object().unwrap());
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_arr = arr_jsonb.as_raw();
assert!(!raw_arr.is_object().unwrap());
let null_jsonb = "null".parse::<OwnedJsonb>().unwrap();
let raw_null = null_jsonb.as_raw();
assert!(!raw_null.is_object().unwrap());
// Invalid JSONB
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]);
let invalid_raw_jsonb = invalid_jsonb.as_raw();
let result = invalid_raw_jsonb.is_object();
assert!(result.is_err());Source§impl<'a> RawJsonb<'a>
impl<'a> RawJsonb<'a>
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Converts the JSONB value to a JSON string.
This function serializes the JSONB value into a human-readable JSON string representation. If the JSONB data is invalid, treat it as a text JSON string and return directly. If the JSONB data is empty, return a JSON null for compatibility.
§Returns
String- The JSON string representation of the value.
§Examples
use lance_jsonb::OwnedJsonb;
let arr_jsonb = "[1, 2, 3]".parse::<OwnedJsonb>().unwrap();
let raw_jsonb = arr_jsonb.as_raw();
assert_eq!(raw_jsonb.to_string(), "[1,2,3]");
let obj_jsonb = r#"{"a": 1, "b": "hello"}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = obj_jsonb.as_raw();
assert_eq!(raw_jsonb.to_string(), r#"{"a":1,"b":"hello"}"#);
let num_jsonb = "123.45".parse::<OwnedJsonb>().unwrap();
let raw_jsonb = num_jsonb.as_raw();
assert_eq!(raw_jsonb.to_string(), "123.45");
let string_jsonb = r#""hello, world!""#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = string_jsonb.as_raw();
assert_eq!(raw_jsonb.to_string(), r#""hello, world!""#);
let true_jsonb = "true".parse::<OwnedJsonb>().unwrap();
let raw_jsonb = true_jsonb.as_raw();
assert_eq!(raw_jsonb.to_string(), "true");
// Example with invalid JSONB data (fallback to treat as text JSON string)
let invalid_jsonb = OwnedJsonb::from(vec![1, 2, 3, 4]); // Invalid binary JSONB
let invalid_raw_jsonb = invalid_jsonb.as_raw();
// It will treat as text JSON string.
assert_eq!(invalid_raw_jsonb.to_string(), "\u{1}\u{2}\u{3}\u{4}");Trait Implementations§
Source§impl AsRef<[u8]> for RawJsonb<'_>
Allows accessing the underlying byte slice as a reference.
This enables easy integration with functions that expect a &u8.
impl AsRef<[u8]> for RawJsonb<'_>
Allows accessing the underlying byte slice as a reference. This enables easy integration with functions that expect a &u8.
impl<'a> Copy for RawJsonb<'a>
impl Eq for RawJsonb<'_>
Source§impl<'a> From<&'a [u8]> for RawJsonb<'a>
Converts a borrowed byte slice into a RawJsonb.
This provides a convenient way to create a RawJsonb from existing data without copying.
impl<'a> From<&'a [u8]> for RawJsonb<'a>
Converts a borrowed byte slice into a RawJsonb. This provides a convenient way to create a RawJsonb from existing data without copying.
Source§impl Ord for RawJsonb<'_>
Implements Ord for RawJsonb, allowing comparison of two RawJsonb values using the total ordering.
This implementation leverages the PartialOrd implementation, returning Ordering::Equal for incomparable values.
impl Ord for RawJsonb<'_>
Implements Ord for RawJsonb, allowing comparison of two RawJsonb values using the total ordering.
This implementation leverages the PartialOrd implementation, returning Ordering::Equal for incomparable values.
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for RawJsonb<'_>
Implements PartialOrd for RawJsonb, allowing comparison of two RawJsonb values.
impl PartialOrd for RawJsonb<'_>
Implements PartialOrd for RawJsonb, allowing comparison of two RawJsonb values.
The comparison logic handles different JSONB types (scalar, array, object) and considers null values. The ordering is defined as follows:
- Null is considered greater than any other type.
- Scalars are compared based on their type and value (String > Number > Boolean > ExtensionValue).
- Arrays are compared element by element.
- Objects are compared based on their keys and values.
- Arrays are greater than objects and scalars.
- Objects are greater than scalars.
- If the types are incompatible, None is returned.