2 releases

0.1.1 May 3, 2023
0.1.0 May 3, 2023

#2265 in Encoding

Download history 1252/week @ 2024-08-30 2211/week @ 2024-09-06 787/week @ 2024-09-13 941/week @ 2024-09-20 900/week @ 2024-09-27 1234/week @ 2024-10-04 1260/week @ 2024-10-11 2220/week @ 2024-10-18 558/week @ 2024-10-25 354/week @ 2024-11-01 460/week @ 2024-11-08 403/week @ 2024-11-15 921/week @ 2024-11-22 545/week @ 2024-11-29 877/week @ 2024-12-06 393/week @ 2024-12-13

2,796 downloads per month
Used in 3 crates (via value-bag-serde1)

Apache-2.0 OR MIT

59KB
1.5K SLoC

serde_buf

rust Latest version Documentation Latest

Like serde_json::Value, but opaque, borrowing, and format-agnostic.

See the docs for more details.


lib.rs:

Generic buffering for serde.

This library provides the Owned and [Ref] types as format-independent buffers for serde. Buffers are guaranteed to serialize in exactly the same way as their original source.

Getting an owned buffer

Any type that implements serde::Serialize can be buffered into an Owned buffer:

use serde_buf::Owned;

// Imagine we have some owned resource...
thread_local! {
static SOME_THREAD_STATIC: RefCell<Option<Owned>> = RefCell::new(None);
}

// ...and some short-lived data
let short_lived: &'_ str = "A string";

// We can create an owned buffer from it...
let buffer = Owned::buffer(short_lived)?;

// ...and stash it in our owned resource
SOME_THREAD_STATIC.with(|ts| *ts.borrow_mut() = Some(buffer));

Getting a borrowed buffer

Borrowed [Ref] buffers may have internally borrowed strings, which makes them incompatible with serde::Serialize. You can construct a [Ref] manually from any underlying source:

use serde_buf::Ref;

// Imagine we have some borrowed datatype
struct MyData<'a> {
id: u64,
content: &'a str,
}

// We can buffer it into a partially owned buffer manually
fn buffer_my_data<'a>(data: &'_ MyData<'a>) -> Ref<'a> {
Ref::record_struct("MyData", [
("id", Ref::u64(data.id)),
("content", Ref::str(data.content)),
])
}

Serializing a buffer

Once you've got an Owned or [Ref] buffer, you can then later use their own serde::Serialize implementations to encode in some format:

#[derive(Serialize)]
struct MyData<'a> {
id: u64,
content: &'a str,
}

let data = MyData {
id: 42,
content: "Some content",
};

let buffer = Owned::buffer(&data)?;

let data_json = serde_json::to_string(&data)?;
let buffer_json = serde_json::to_string(&buffer)?;

assert_eq!(data_json, buffer_json);

Deserializing from a buffer

Values can also be deserialized directly from an Owned or [Ref] buffer through their serde::de::IntoDeserializer implementations:

#[derive(Deserialize, Debug, PartialEq)]
struct MyData<'a> {
id: u64,
content: &'a str,
}

let data = MyData {
id: 42,
content: "Some content",
};

let buffer: Ref = buffer_my_data(&data);

let deserialized = MyData::deserialize(buffer.into_deserializer())?;

assert_eq!(data, deserialized);

Deserializing directly to a buffer

The [Ref] and Owned types don't implement serde::Deserialize and can't be deserialized directly. This is because serde relies on hints from the target type to know how to interpret enum variants. If a type implements both serde::Serialize and serde::Deserialize then you can first deserialize to that concrete type, and then buffer it:

#[derive(Serialize, Deserialize)]
enum MyData<'a> {
Short(&'a str),
Full { id: u64, content: &'a str },
}

let json = data_json();

let buffer = Owned::buffer(&serde_json::from_str::<MyData>(&json)?)?;

Dependencies

~100–335KB