Cogs and Levers A blog full of technical stuff

serde

Rust is strongly typed. The outside world is not. Configuration files, HTTP payloads, JSON blobs, environment variables — they’re all loosely structured text. Eventually, you need to convert that into real Rust types.

That’s what serde does.

It turns external data formats into structured Rust types — and back again — without runtime reflection.

What Problem Does serde Solve?

Serialization and deserialization without:

  • runtime type inspection
  • fragile string parsing
  • manual boilerplate

serde works at compile time using derives. It generates the glue code that maps formats (like JSON) into your structs.

No magic. Just code generation.

Minimal Example: Deserialize JSON

Cargo.toml

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

main.rs

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    host: String,
    port: u16,
}

fn main() {
    let json = r#"
        { "host": "localhost", "port": 8080 }
    "#;

    let config: Config = serde_json::from_str(json).unwrap();

    println!("{config:?}");
}

That’s it.

No parsing logic. No manual mapping. No reflection.

What’s Actually Happening?

The derive macro generates implementations of:

  • serde::Serialize
  • serde::Deserialize

Formats like serde_json, toml, or bincode plug into those traits.

Serde separates:

  • Data model
  • Format implementation

This is why the ecosystem is enormous.

Where It Fits

Use serde when:

  • You read configuration files
  • You consume HTTP APIs
  • You persist data
  • You build network protocols

It’s foundational in modern Rust.

Trade-offs

Pros

  • Zero runtime reflection
  • Extremely fast
  • Works across formats
  • Minimal boilerplate

Cons

  • Derive-heavy codebases can hide complexity
  • Deeply nested types can become verbose
  • Custom serialization requires understanding traits

Should You Use It?

If your program talks to the outside world:

Yes.

In modern Rust, avoiding serde is the unusual choice.