#serialization #serde #no-std #derive-deserialize

no-std serde_state

为serde提供状态化序列化和反序列化

12个版本

使用旧的Rust 2015

0.4.8 2021年2月5日
0.4.7 2019年11月9日
0.4.6 2019年6月24日
0.4.4 2018年6月29日
0.3.0 2017年7月18日

#1767 in 编码

Download history 43/week @ 2024-03-13 55/week @ 2024-03-20 54/week @ 2024-03-27 64/week @ 2024-04-03 41/week @ 2024-04-10 44/week @ 2024-04-17 43/week @ 2024-04-24 45/week @ 2024-05-01 42/week @ 2024-05-08 100/week @ 2024-05-15 89/week @ 2024-05-22 135/week @ 2024-05-29 85/week @ 2024-06-05 46/week @ 2024-06-12 36/week @ 2024-06-19 23/week @ 2024-06-26

每月195次下载
6 个crate(3 直接)中使用

MIT/Apache

150KB
4K SLoC

../README.md


lib.rs:

serde_state

serde_state 是一个crate,它扩展了正常的 DeserializeSerialize traits,允许将状态传递给每个序列化或反序列化的值。

示例

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);
    }
}

依赖项

~110–345KB