Crate serde_plain

Source
Expand description

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:

use serde::Deserialize;

#[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");

Macros§

derive_deserialize_from_fromstr
Derives Deserialize for a type that implements FromStr.
derive_display_from_serialize
Implements Display for a type that forwards to Serialize.
derive_fromstr_from_deserialize
Implements FromStr for a type that forwards to Deserialize.
derive_serialize_from_display
Derives Serialize a type that implements Display.

Structs§

Deserializer
A simple deserializer that works with plain strings.
Serializer
A simple serializer that can dump out strings.

Enums§

Error
Errors created from this crate.

Functions§

from_str
Deserialize an instance of type T from a string of plain text.
to_string
Serialize the given data value as a plain string.