serde-extras

Crates.ioserde-extras
lib.rsserde-extras
version0.1.1
created_at2025-06-29 18:08:22.736762+00
updated_at2025-06-29 18:33:05.124225+00
descriptionA lightweight crate providing utilities for serializing and deserializing types using their string representations with Serde.
homepagehttps://github.com/cygnus9/serde-extras
repositoryhttps://github.com/cygnus9/serde-extras
max_upload_size
id1730998
size24,541
(cygnus9)

documentation

https://docs.rs/serde-extras

README

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.

Commit count: 0

cargo fmt