serde_with

Custom de/serialization helpers for Rust's serde, via the with and serde_as annotations

Library
Cargo
v3.24.0
865 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
87 /100 Excellent
Development Activity 100
Maintenance 96
Community 64
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
79 /100 Good
Architecture 80
Code Quality 88
Innovation 78
Learning Curve 68

serde_with provides a large library of custom serialization/deserialization adapters for use with serde’s #[serde(with = "...")] attribute and its own more flexible #[serde_as] annotation. It handles common pain points serde doesn’t solve out of the box: converting via Display/FromStr, serializing arrays larger than 32 elements or with const generics, skipping empty Option fields en masse, adding field-name prefixes/suffixes, and parsing delimiter-separated strings into collections.

Maintained by Jonas Bushart since 2017, it’s one of the most widely used serde companion crates in the Rust ecosystem, with well over 300 million total downloads and adapters for many popular crates (chrono, hashbrown, indexmap, and more) gated behind feature flags.

What You Get

  • #[serde_as] attribute with dozens of built-in adapters: DisplayFromStr, Hex, Base64, DurationSeconds, Bytes, and more
  • Support for arrays larger than 32 elements and const-generic arrays, which serde’s derive macros can’t handle natively
  • #[skip_serializing_none] to skip every empty Option field on a struct without annotating each one individually
  • with_prefix!/with_suffix! macros to add a name prefix/suffix to all fields of a struct without touching its de/serialize impls
  • StringWithSeparator for parsing delimiter-separated strings (e.g. comma-separated tags) into typed collections
  • Optional feature-gated integrations for chrono, hashbrown, indexmap, and other common crates’ types

Common Use Cases

  • Serializing/deserializing types via their Display/FromStr impls (e.g. u8 as a JSON string, url::Url, mime::Mime)
  • Working around serde’s 32-element array limitation, especially with const-generic array sizes
  • Compact JSON APIs that omit null/absent optional fields entirely rather than serializing them as null
  • Converting comma- or delimiter-separated string fields (common in config files and query params) into Vec<T>

Under The Hood

Architecture - The repo is a Cargo workspace of three crates: serde_with (the public API, ~22.5k lines across adapters organized by target format like base64.rs, hex.rs, chrono_0_4.rs), serde_with_macros (the proc-macro crate implementing #[serde_as], #[skip_serializing_none], and the derive-macro plumbing), and serde_with_test (an internal integration-test harness); the serde_as attribute works by expanding to type-level wrapper structs that implement Serialize/Deserialize and delegate to the underlying value via the requested conversion. Tech Stack - Built on serde/serde_derive with numerous optional feature-gated dependencies (chrono, hashbrown across several versions, indexmap, base64, hex) so consumers only compile in the adapters they use; edition 2021, MSRV 1.88, dual MIT/Apache-2.0 licensed. Code Quality - Extensive per-adapter integration tests (tests/base64.rs, tests/hex.rs, tests/chrono_0_4.rs, plus a dedicated tests/derives subdirectory), workspace-wide Clippy lints (clippy.toml) and strict rustfmt config, CII Best Practices badge, and Bors-managed merge queue (bors.toml) — signals of a mature, process-disciplined project with 1,784 commits over eight years. API Design - The serde_as annotation’s type-mirroring syntax (Vec<DisplayFromStr>, Option<[_; M]>) is more expressive and composable than serde’s plain with attribute, letting nested nested generic types each specify their own conversion, at the cost of a steeper initial learning curve for developers unfamiliar with the type-mirroring convention.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers