pub struct OwnedJsonb { /* private fields */ }Expand description
Represents a JSONB data that owns its underlying data.
This struct provides ownership over the binary JSONB representation.
OwnedJsonb is primarily used to create JSONB data from other data types (such as JSON String).
However, for most operations, it’s necessary to convert an OwnedJsonb to a RawJsonb using the as_raw() method
to avoid unnecessary copying and to take advantage of the performance benefits of the read-only access of the RawJsonb.
Implementations§
Source§impl OwnedJsonb
impl OwnedJsonb
Sourcepub fn new(data: Vec<u8>) -> OwnedJsonb
pub fn new(data: Vec<u8>) -> OwnedJsonb
Sourcepub fn as_raw(&self) -> RawJsonb<'_>
pub fn as_raw(&self) -> RawJsonb<'_>
Creates a RawJsonb view of the owned data.
This is useful for passing the data to functions that expect a RawJsonb.
This does not transfer ownership.
§Returns
A RawJsonb instance referencing the owned data.
Sourcepub fn to_vec(self) -> Vec<u8> ⓘ
pub fn to_vec(self) -> Vec<u8> ⓘ
Consumes the OwnedJsonb and returns the underlying Vec
§Returns
The underlying Vec<u8> containing the JSONB data.
Sourcepub fn build_array<'a>(
raw_jsonbs: impl IntoIterator<Item = RawJsonb<'a>>,
) -> Result<OwnedJsonb, Error>
pub fn build_array<'a>( raw_jsonbs: impl IntoIterator<Item = RawJsonb<'a>>, ) -> Result<OwnedJsonb, Error>
Builds a JSONB array from a collection of RawJsonb values.
This function constructs a new JSONB array from an iterator of RawJsonb values.
The resulting OwnedJsonb represents the binary encoding of the array.
§Arguments
items- An iterator ofRawJsonbvalues representing the elements of the array.
§Returns
Ok(OwnedJsonb)- The newly created JSONB array.Err(Error)- If any of the inputRawJsonbvalues are invalid or if an error occurs during array construction.
§Examples
use jsonb::OwnedJsonb;
use jsonb::RawJsonb;
// Create some RawJsonb values
let owned_num = "1".parse::<OwnedJsonb>().unwrap();
let owned_str = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let owned_arr = "[1,2,3]".parse::<OwnedJsonb>().unwrap();
// Build the array
let raw_jsonbs = vec![owned_num.as_raw(), owned_str.as_raw(), owned_arr.as_raw()];
let array_result = OwnedJsonb::build_array(raw_jsonbs.into_iter());
assert!(array_result.is_ok());
let array = array_result.unwrap();
// Convert to string for easy verification
assert_eq!(array.to_string(), "[1,\"hello\",[1,2,3]]");
// Example with an empty iterator
let empty_array =
OwnedJsonb::build_array(<[RawJsonb<'_>; 0] as IntoIterator>::into_iter([])).unwrap();
assert_eq!(empty_array.to_string(), "[]");
// Example with invalid input (this will cause an error)
let invalid_data = OwnedJsonb::new(vec![1, 2, 3, 4]);
let result = OwnedJsonb::build_array([invalid_data.as_raw()].into_iter());
assert!(result.is_err());Sourcepub fn build_object<'a, K: AsRef<str>>(
items: impl IntoIterator<Item = (K, RawJsonb<'a>)>,
) -> Result<OwnedJsonb, Error>
pub fn build_object<'a, K: AsRef<str>>( items: impl IntoIterator<Item = (K, RawJsonb<'a>)>, ) -> Result<OwnedJsonb, Error>
Builds a JSONB object from a collection of key-value pairs.
This function constructs a new JSONB object from an iterator of key-value pairs. The keys are strings, and the values are RawJsonb values.
The resulting OwnedJsonb represents the binary encoding of the object.
§Arguments
items- An iterator of(K, &'a RawJsonb<'a>)tuples, whereKis a type that can be converted into a string slice (AsRef<str>) representing the key, and the second element is aRawJsonbrepresenting the value.
§Returns
Ok(OwnedJsonb)- The newly created JSONB object.Err(Error)- If any of the inputRawJsonbvalues are invalid, if contain duplicate keys, or if an error occurs during object construction.
§Examples
use jsonb::OwnedJsonb;
use jsonb::RawJsonb;
// Create some RawJsonb values
let owned_num = "1".parse::<OwnedJsonb>().unwrap();
let owned_str = r#""hello""#.parse::<OwnedJsonb>().unwrap();
let owned_arr = "[1,2,3]".parse::<OwnedJsonb>().unwrap();
// Build the object
let raw_jsonbs = vec![
("a", owned_num.as_raw()),
("b", owned_str.as_raw()),
("c", owned_arr.as_raw()),
];
let object_result = OwnedJsonb::build_object(raw_jsonbs.into_iter());
assert!(object_result.is_ok());
let object = object_result.unwrap();
// Convert to string for easy verification
assert_eq!(object.to_string(), r#"{"a":1,"b":"hello","c":[1,2,3]}"#);
// Example with an empty iterator
let empty_object =
OwnedJsonb::build_object(<[(&str, RawJsonb<'_>); 0] as IntoIterator>::into_iter([]))
.unwrap();
assert_eq!(empty_object.to_string(), "{}");
// Example with invalid value
let invalid_data = OwnedJsonb::new(vec![1, 2, 3, 4]);
let result = OwnedJsonb::build_object([("a", invalid_data.as_raw())].into_iter());
assert!(result.is_err());Trait Implementations§
Source§impl AsRef<[u8]> for OwnedJsonb
Allows accessing the underlying byte slice as a reference.
This enables easy integration with functions that expect a &[u8].
impl AsRef<[u8]> for OwnedJsonb
Allows accessing the underlying byte slice as a reference.
This enables easy integration with functions that expect a &[u8].
Source§impl Clone for OwnedJsonb
impl Clone for OwnedJsonb
Source§fn clone(&self) -> OwnedJsonb
fn clone(&self) -> OwnedJsonb
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OwnedJsonb
impl Debug for OwnedJsonb
Source§impl Display for OwnedJsonb
Implements the Display trait, allowing OwnedJsonb to be formatted as a string using the {} format specifier.
impl Display for OwnedJsonb
Implements the Display trait, allowing OwnedJsonb to be formatted as a string using the {} format specifier.
Source§impl From<&[u8]> for OwnedJsonb
Creates an OwnedJsonb from a borrowed byte slice. The byte slice is copied into a new Vec<u8>.
impl From<&[u8]> for OwnedJsonb
Creates an OwnedJsonb from a borrowed byte slice. The byte slice is copied into a new Vec<u8>.
Source§impl From<Vec<u8>> for OwnedJsonb
Creates an OwnedJsonb from a Vec<u8>. This is a simple ownership transfer.
impl From<Vec<u8>> for OwnedJsonb
Creates an OwnedJsonb from a Vec<u8>. This is a simple ownership transfer.
Source§impl FromStr for OwnedJsonb
Parses a string into an OwnedJsonb.
The string is parsed into a JSON value, then encoded into the binary JSONB format.
impl FromStr for OwnedJsonb
Parses a string into an OwnedJsonb.
The string is parsed into a JSON value, then encoded into the binary JSONB format.
Source§impl Ord for OwnedJsonb
Implements Ord for OwnedJsonb, allowing comparison of two OwnedJsonb values using the total ordering.
This implementation leverages the PartialOrd implementation, returning Ordering::Equal for incomparable values.
impl Ord for OwnedJsonb
Implements Ord for OwnedJsonb, allowing comparison of two OwnedJsonb values using the total ordering.
This implementation leverages the PartialOrd implementation, returning Ordering::Equal for incomparable values.
Source§impl PartialEq for OwnedJsonb
impl PartialEq for OwnedJsonb
Source§impl PartialOrd for OwnedJsonb
Implements PartialOrd for OwnedJsonb, allowing comparison of two OwnedJsonb values.
impl PartialOrd for OwnedJsonb
Implements PartialOrd for OwnedJsonb, allowing comparison of two OwnedJsonb 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).
- 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.
impl Eq for OwnedJsonb
Auto Trait Implementations§
impl Freeze for OwnedJsonb
impl RefUnwindSafe for OwnedJsonb
impl Send for OwnedJsonb
impl Sync for OwnedJsonb
impl Unpin for OwnedJsonb
impl UnwindSafe for OwnedJsonb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.