#serialization #codec #deserialize #decoding #multiple #interface

simple_serde

A wrapper around multiple serde libraries making it easy to encode and decode multiple types

13 releases (3 stable)

2.0.0 May 7, 2024
1.1.3 May 7, 2024
1.0.0-rc1 Apr 15, 2022
0.3.8 Jan 27, 2022
0.1.0 Jan 21, 2022

#235 in Encoding

Download history 33/week @ 2024-07-23 35/week @ 2024-09-24 53/week @ 2024-10-01

956 downloads per month
Used in 2 crates

MIT license

75KB
1.5K SLoC

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 std::ops::Deref;
use serde::Serialize;
#[macro_use]
use serde_derive;
use simple_serde::{Encoded, SimpleEncoder, TryToString};

#[derive(Serialize)]
struct Foo {
    bar: String,
}

let my_foo = Foo {
  bar: "foobar".to_string(),
};

let encoded: Encoded = my_foo
  .encode("yaml")
  .expect("Should have been encoded in yaml");

assert_eq!(
    &vec![45, 45, 45, 10, 98, 97, 114, 58, 32, 102, 111, 111, 98, 97, 114, 10],
    encoded.deref()
);
assert_eq!(r#"---
bar: foobar
"#, encoded.try_to_string().unwrap())

Deserialization/Decode example

use std::ops::Deref;
use serde::Deserialize;
#[macro_use]
use serde_derive;
use simple_serde::{Decoded, SimpleDecoder};

#[derive(Deserialize, Debug, PartialEq)]
struct Foo {
    bar: String,
}

let my_foo = Foo {
  bar: "foobar".to_string(),
};

let v_u8_data = &vec![45, 45, 45, 10, 98, 97, 114, 58, 32, 102, 111, 111, 98, 97, 114, 10];
let string_data = r#"---
bar: foobar
"#;

let decoded_from_v_u8: Decoded<Foo> = v_u8_data.decode("yaml").expect("Should have decoded the Vec<u8>");
let decoded_from_string: Decoded<Foo> = string_data.decode("yaml").expect("Should have decoded the String");

assert_eq!(
    Foo{bar: "foobar".to_string()},
    decoded_from_v_u8.into()
);
assert_eq!(
    Foo{bar: "foobar".to_string()},
    decoded_from_string.into()
);

Contribute

Any merge requests are welcomed!

License

MIT

Dependencies

~8–19MB
~286K SLoC