#serialization #serde #deserializing #another #traits #derive #serde-roundtrip

macro serde_roundtrip_derive

Deriving a trait for when serde supports serializing at one type and deserializing as another

3 releases

Uses old Rust 2015

0.1.2 Mar 29, 2017
0.1.1 Mar 29, 2017
0.1.0 Mar 29, 2017

#30 in #deserializing


Used in serde_roundtrip

MPL-2.0 license

10KB
198 lines

serde_roundtrip

A trait for when serde supports serializing at one type and deserializing as another.

The main trait is S: RoundTrip<T> which means that data of type S can be serialized using serde, then safely deserialized at type T. This allows serialization to be safe without taking ownership of the data, for example serializing a &[&str] and deserializing a Vec<String>.

The RoundTrip<T> trait provides a method fn round_trip(&self) -> T, which has the same semantics as serializing then deserializing. This allows serialization to be short-circuited in the case that an in-memory representation can be used.

The RoundTrip trait is implemented for the types for which serde provides a serialization. For user-defined types, the serde_roundtrip_derive crate provides a derive(RoundTrip).

For example:

// A type which can be round-tripped by serde.
// The type might be changed by round-tripping,
// for example a message might be sent as a `Msg<&str>`
// and arrive as a `Msg<String>`.
#[derive(Serialize, Deserialize, RoundTrip, Debug, PartialEq, Eq)]
struct Msg<T>(T);

fn main() {
    // Create a message at type `Msg<&str>`
    let msg: Msg<&'static str> = Msg("hello");

    // Round-trip it via JSON.
    let json = serde_json::to_string(&msg).unwrap();
    let round_tripped: Msg<String> = serde_json::from_str(&*json).unwrap();

    // This is the same as calling the `round_trip()` method.
    assert_eq!(round_tripped, msg.round_trip());
}

Dependencies

~1.5MB
~40K SLoC