#display #from-str #serialization #serde #plain-text #serializer-deserializer

serde_plain2

Fork of serde_plain. A restricted plain text serializer for serde

1 unstable release

new 0.1.0 Dec 5, 2024

#677 in Encoding

Download history 147/week @ 2024-12-02

147 downloads per month

MIT license

26KB
485 lines

serde-plain2

This is a personal fork of 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.

From String

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

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

This is particularly useful if enums are in use:

use serde::Deserialize;

#[derive(Deserialize, Debug, PartialEq, Eq)]
pub enum MyEnum {
    VariantA,
    VariantB,
}

assert_eq!(serde_plain2::from_str::<MyEnum>("VariantA").unwrap(), MyEnum::VariantA);

To String

The inverse is also possible with to_string:

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

Credits

Dependencies

~0.4–1MB
~21K SLoC