Simple Serde
Simple serde is as its said, a simplified implementation of multiple repositories for serialization and deserialization.
In Short the goal is to have a single tool for serialization and deserialization, with a common interface.
Usage
Simple Serde uses .encode and .decode for encoding and decoding. Decode can be done on any
Vec<u8> or &[u8] this allows for the cleanest implementation.
The same goes for anything that needs to be serialized/encoded. Any type that implements the
#[derive(Serialize)] can easily be encoded using .encode
Encode/Decode
.encode and .decode both takes a ContentType which defines what you are encoding/decoding
from/to.
an example would be [some Vec<u8>].decode("bson") or my_struct.encode("bson").
This is possible as ContentType implements the TryFrom trait for &str, String.
In case the implementation is unable to decode what type you are trying to encode/decode from/to
an Err result with Error::UnknownContentTypeMatchFromStr will be returned from the
encoder/decoder
Anything coming out of the encoder will be of type Vec<u8> further the Vec<u8> is wrapped in
a struct called Encoded this allow for further simplifications on implementation like,
TryToString which will automatically try to convert Encoded to a String, in addition
Encoded had implemented the Deref and DerefMut traits to make it easier to gain access to
encapsulated data.
Supported formats
- Bson
- Cbor
- FlexBuffers
- Json
- Json5
- Lexpr
- MessagePack
- Pickle
- Postcard
- Ron
- Toml
- Url
- Yaml
- Xml (Awaiting serde-xml-rs v. >0.51)
further all string definitions of ContentType is case insensitive, and has an alternate
application/[format]application/x-[format]
Serialization/Encode example
use Deref;
use Serialize;
use serde_derive;
use ;
let my_foo = Foo ;
let encoded: Encoded = my_foo
.encode
.expect;
assert_eq!;
assert_eq!
Deserialization/Decode example
use Deref;
use Deserialize;
use serde_derive;
use ;
let my_foo = Foo ;
let v_u8_data = &vec!;
let string_data = r#"---
bar: foobar
"#;
let decoded_from_v_u8: = v_u8_data.decode.expect;
let decoded_from_string: = string_data.decode.expect;
assert_eq!;
assert_eq!;
Contribute
Any merge requests are welcomed!
License
MIT