#serde #serialization #from-str #display

serde_plain

A restricted plain text serializer for serde

3 releases (breaking)

Uses old Rust 2015

0.3.0 Mar 19, 2018
0.2.0 Mar 13, 2018
0.1.0 Mar 12, 2018

#264 in Encoding

Download history 5757/week @ 2021-01-21 5319/week @ 2021-01-28 5869/week @ 2021-02-04 6309/week @ 2021-02-11 6516/week @ 2021-02-18 5609/week @ 2021-02-25 5783/week @ 2021-03-04 5809/week @ 2021-03-11 5630/week @ 2021-03-18 5407/week @ 2021-03-25 4775/week @ 2021-04-01 3252/week @ 2021-04-08 3788/week @ 2021-04-15 5745/week @ 2021-04-22 9460/week @ 2021-04-29 6117/week @ 2021-05-06

22,795 downloads per month
Used in 68 crates (21 directly)

MIT/Apache

20KB
419 lines

Serde Plain

This crate implements a plain text serializer and deserializer. It can only serialize and deserialize primitives and derivatives thereof (like basic enums or newtypes). It internally uses the FromStr and Display trait to convert objects around.


lib.rs:

This crate implements a plain text serializer and deserializer. It can only serialize and deserialize primitives and derivatives thereof (like basic enums or newtypes). It internally uses the FromStr and Display trait to convert objects around.

The idea of this crate is that you can use the serde system to implement FromStr or Display for your own types based on the how serde would handle the type.

From String

To parse a value from a string the from_str helper can be used:

assert_eq!(serde_plain::from_str::<i32>("42").unwrap(), 42);

This is particularly useful if enums are in use:

# #[macro_use] extern crate serde_derive;
# extern crate serde_plain;
# fn main() {
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub enum MyEnum {
    VariantA,
    VariantB,
}

assert_eq!(serde_plain::from_str::<MyEnum>("VariantA").unwrap(), MyEnum::VariantA);
# }

To String

The inverse is also possible with to_string:

assert_eq!(serde_plain::to_string(&true).unwrap(), "true");

Dependencies

~150–270KB