11 releases

0.4.7 Nov 9, 2019
0.4.6 Jun 24, 2019
0.4.4 Jun 29, 2018
0.4.3 Nov 29, 2017
0.3.0 Jul 18, 2017

#101 in Encoding

Download history 84/week @ 2019-12-02 107/week @ 2019-12-09 30/week @ 2019-12-16 20/week @ 2019-12-23 22/week @ 2019-12-30 42/week @ 2020-01-06 77/week @ 2020-01-13 1/week @ 2020-01-20 19/week @ 2020-01-27 20/week @ 2020-02-03 30/week @ 2020-02-10 51/week @ 2020-02-17 38/week @ 2020-02-24 8/week @ 2020-03-02 55/week @ 2020-03-09 78/week @ 2020-03-16

337 downloads per month
Used in 6 crates (3 directly)

MIT/Apache

155KB
4K SLoC

../README.md


lib.rs:

serde_state

serde_state is a crate which extends the normal Deserialize and Serialize traits to allow state to be passed to every value which is serialized or deserialized.

Example

extern crate serde_json;
extern crate serde_state as serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_derive_state;

use std::borrow::BorrowMut;
use std::cell::Cell;
use serde::ser::{Serialize, Serializer, SerializeState};
use serde::de::{Deserialize, Deserializer, DeserializeState};

#[derive(Deserialize, Serialize)]
struct Inner;

impl SerializeState<Cell<i32>> for Inner {

    fn serialize_state<S>(&self, serializer: S, seed: &Cell<i32>) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        seed.set(seed.get() + 1);
        self.serialize(serializer)
    }
}

impl<'de, S> DeserializeState<'de, S> for Inner where S: BorrowMut<i32> {

    fn deserialize_state<D>(seed: &mut S, deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        *seed.borrow_mut() += 1;
        Self::deserialize(deserializer)
    }
}

#[derive(SerializeState, DeserializeState)]

// `serialize_state` or `deserialize_state` is necessary to tell the derived implementation which
// seed that is passed
#[serde(serialize_state = "Cell<i32>")]

// `de_parameters` can be used to specify additional type parameters for the derived instance
#[serde(de_parameters = "S")]
#[serde(bound(deserialize = "S: BorrowMut<i32>"))]
#[serde(deserialize_state = "S")]
struct Struct {
    // The `serialize_state` attribute must be specified to use seeded serialization
    #[serde(serialize_state)]
    // The `deserialize_state` attribute must be specified to use seeded deserialization
    #[serde(deserialize_state)]
    value: Inner,

    // The `seed` attribute can be used to specify `deserialize_state` and `serialize_state`
    // simultaneously
    #[serde(state)]
    value2: Inner,

    // If no attributes are specified then normal serialization and/or deserialization is used
    value3: Inner,

    // The `[de]serialize_state_with` attribute can be used to specify a custom function which
    // does the serialization or deserialization
    #[serde(serialize_state_with = "serialize_inner")]
    value4: Inner,
}

fn serialize_inner<S>(self_: &Inner, serializer: S, seed: &Cell<i32>) -> Result<S::Ok, S::Error>
    where S: Serializer
{
    seed.set(seed.get() + 10);
    self_.serialize(serializer)
}

fn main() {
    let s = Struct {
        value: Inner,
        value2: Inner,
        value3: Inner,
        value4: Inner,
    };

    let mut buffer = Vec::new();
    {
        let mut serializer = serde_json::Serializer::pretty(&mut buffer);
        let seed = Cell::new(0);
        s.serialize_state(&mut serializer, &seed).unwrap();
        assert_eq!(seed.get(), 12);
    }
    {
        let mut deserializer = serde_json::Deserializer::from_slice(&buffer);
        let mut seed = 0;
        Struct::deserialize_state(&mut seed, &mut deserializer).unwrap();
        assert_eq!(seed, 2);
    }
}

Dependencies

~180–410KB