to_owned_jsonb

Function to_owned_jsonb 

Source
pub fn to_owned_jsonb<T>(value: &T) -> Result<OwnedJsonb, Error>
where T: Serialize,
Expand description

Serializes a Rust data structure into an OwnedJsonb using Serde.

This function takes a Rust type T that implements the Serialize trait and serializes it into an OwnedJsonb, which is a struct containing a Vec<u8> representing the JSONB data. It uses a custom Serializer to handle the serialization process.

§Arguments

  • value: A reference to the value of type T to be serialized.

§Type Parameters

  • T: The Rust type to serialize. This type must implement the serde::ser::Serialize trait.

§Returns

  • Ok(OwnedJsonb): If the serialization is successful, returns an OwnedJsonb containing the serialized JSONB data.
  • Err(e): If any Serde serialization error occurs.

§Examples

use jsonb::to_owned_jsonb;
use jsonb::OwnedJsonb;
use serde::Serialize;

#[derive(Serialize, Debug)]
struct Person {
    name: String,
    age: u32,
}

let person = Person {
    name: "Bob".to_string(),
    age: 42,
};

let owned_jsonb: OwnedJsonb = to_owned_jsonb(&person).unwrap();
assert_eq!(format!("{}", owned_jsonb), "{\"age\":42,\"name\":\"Bob\"}");
println!("JSONB data: {}", owned_jsonb);