Crate serde_extras

Crate serde_extras 

Source
Expand description

§serde-extras

Crates.io Docs.rs

A lightweight crate providing utilities for serializing and deserializing types using their string representations with Serde.

§Features

  • Serialize and deserialize types that implement ToString and FromStr via Serde attributes.
  • Minimal dependencies and small code footprint.

§Usage

Add to your Cargo.toml:

serde-extras = "0.1"

Annotate your struct fields with Serde’s with attribute:

use std::net::IpAddr;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Wrapper {
    #[serde(with = "serde_extras::to_from_str")]
    ip: IpAddr,
}

This will serialize the ip field as a string and deserialize it from a string.

§Example

use std::net::IpAddr;
use serde::{Serialize, Deserialize};
use serde_json;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Wrapper {
    #[serde(with = "serde_extras::to_from_str")]
    ip: IpAddr,
}

let w = Wrapper { ip: IpAddr::V4("127.0.0.1".parse().unwrap()) };
let json = serde_json::to_string(&w).unwrap();
assert_eq!(json, r#"{"ip":"127.0.0.1"}"#);
let de: Wrapper = serde_json::from_str(&json).unwrap();
assert_eq!(de, w);

§Comparison

This crate is a lightweight alternative to a small portion of serde_with. If you only need simple string-based (de)serialization helpers, serde-extras may be a better fit. For more advanced features, consider using serde_with.

§License

This project is licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in serde-extras by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Modules§

opt_to_from_str
Utilities for serializing and deserializing types using their string representations.
to_from_str
Utilities for serializing and deserializing types using their string representations.