Serializable Enum
Overview
Provides two macros to facilitate easier serialization / deserialization of enums with variants
having no data. The default serialization for serde when serializing enums with no data is of the
form: {"Variant": []}. While this works for most use cases, you may want the enum to be
serialized as "variant" instead. The two macros in this crate help make this
serialization/deserialization easier.
These macros are designed to be used with serde only.
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
And to your crate:
extern crate serializable_enum;
Example
Consider this struct:
Assume an instance of Post:
let p = Post ;
Upon serializing Post you want the following output (json):
Using the macros in this crate, we can achieve this through the following (assuming
implementation of Post above):
extern crate serde;
extern crate serde_json;
extern crate serializable_enum;
// You will need display implemented for Error (you should already have this).
serializable_enum!
impl_as_ref_from_str!
serializable_enum sets up the serde serialization and deserialization using the visitor type
provided, in this case ContentFormatVisitor.
impl_as_ref_from_str provides implementations
for AsRef and FromStr traits for the enum using the mappings provided, which are used for
serialization and deserialization. Error::Parse is a variant of an Error enum defined in your
crate with String data. This variant is used as the Err type for
FromStr.
Note: the serializable_enum macro invocation requires:
- Doc-comments for each variant.
For more details, head over to the documentation.
License
This library is distributed under similar terms to Rust: dual licensed under the MIT license and the Apache license (version 2.0).
See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.