#serde #serialization #no_std

no-std serde_state

Stateful serialization/deserialization for serde

8 releases

0.4.4 Jun 29, 2018
0.4.3 Nov 29, 2017
0.4.1 Sep 5, 2017
0.4.0 Aug 5, 2017
0.1.0 Jul 16, 2017

#30 in #serde

Download history 97/week @ 2018-08-13 18/week @ 2018-08-20 30/week @ 2018-08-27 32/week @ 2018-09-03 68/week @ 2018-09-10 104/week @ 2018-09-17 188/week @ 2018-09-24 75/week @ 2018-10-01 87/week @ 2018-10-08 125/week @ 2018-10-15 298/week @ 2018-10-22 60/week @ 2018-10-29 90/week @ 2018-11-05

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

MIT/Apache

154KB
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

~469KB