serde-nothing

Crates.ioserde-nothing
lib.rsserde-nothing
version0.1.1
sourcesrc
created_at2022-02-25 15:54:43.551465
updated_at2022-02-25 16:21:40.571587
descriptionSerialize to nothing. Deserialize from nothing
homepagehttps://github.com/zakarumych/serde-nothing
repositoryhttps://github.com/zakarumych/serde-nothing
max_upload_size
id539497
size41,989
development (github:dabbotorg:development)

documentation

https://docs.rs/serde-nothing

README

serde-nothing

crates docs actions MIT/Apache loc

This crate defines "nothing" in terms of serde data model and allows checking values and create such values using only Serialize and Deserialize traits.

Motivation

This crate is designed to generalize serialization pattern where struct fields with None/empty/default values are skipped on serialization and constructed on deserialization when missing.

Usually this pattern is coded using #[serde(default, skip_serializing_if = "Option::is_none/Vec::is_empty/is_default")]. Where is_default is a function defined as

fn is_default<T: Default + PartialEq>(value: &T) -> bool {
    *value == T::default()
}

The pattern works very well for field with concrete types and generic wrappers like Option, Vec and similar.

But using #[serde(default)] on field with generic type T would require Default bound added to the Deserialize impl. And #[serde(skip_serializing_if = "is_default")] would require T: Default + PartialEq bound added to the Serialize impl.

This crate allows to implement this pattern without additional bounds. Even more, it allows specialize for types that can be skipped. That is, if field has type T that does not even have a value that would be appropriate to skip, the code will continue work correctly and would simply always serialize the field and require the data for deserialization.

Magic? No, science!

This crate provides stateless Nothing type which is a special kind of serde::Serializer or serde::Deserializer. Nothing can be used to serialize and deserialize "nothing" values. "Nothing" values are None, empty collections, units, structs and tuples where all fields are "nothing"

Serializing a "non-nothing" value with Nothing always fails.

As deserializer Nothing would visit most appropriate Visitor method with matching kind of nothingness, like None, empty slice/sequence/map, single-variant enum with nothing in discriminant and nothing in payload, 0 numeric value etc.

User would most probably want to use a shortcut and utilize is_nothing function for serialization and from_nothing function for deserialization.

License

Licensed under either of

at your option.

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 3

cargo fmt