Serialize and deserialize data with Serde in Rust
✓Works with OpenClaudeYou are a Rust developer. The user wants to serialize and deserialize data structures using Serde, Rust's most popular serialization framework.
What to check first
- Verify
serdeandserde_jsonare inCargo.tomlunder[dependencies]withderivefeature enabled - Run
cargo buildto confirm dependencies compile without errors
Steps
- Add
serdewith derive feature andserde_jsontoCargo.toml:serde = { version = "1.0", features = ["derive"] }andserde_json = "1.0" - Import
SerializeandDeserializetraits:use serde::{Serialize, Deserialize}; - Derive both traits on your struct or enum with
#[derive(Serialize, Deserialize, Debug)] - Use
serde_json::to_string()to serialize a struct instance into a JSON string - Use
serde_json::from_str()with type annotation to deserialize a JSON string back into your struct - Handle
Resulttypes — both functions returnResult<T, Error>so use.unwrap(),?, or.expect()for error handling - For custom serialization logic, use
#[serde(rename)]for field names or implement customserializeanddeserializemethods with#[serde(serialize_with = "...")] - Test round-trip serialization: serialize, then deserialize, and verify the data matches
Code
use serde::{Serialize, Deserialize};
use serde_json;
#[derive(Serialize, Deserialize, Debug)]
struct Person {
name: String,
age: u32,
email: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Team {
name: String,
members: Vec<Person>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a struct instance
let person = Person {
name: "Alice".to_string(),
age: 30,
email: "alice@example.com".to_string(),
};
// Serialize to JSON string
let json_string = serde_json::to_string(&person)?;
println!("Serialized: {}", json_string);
// Output: Serialized: {"name":"Alice","age":30,"email":"alice@example.com"}
// Deserialize from JSON string
let deserialized: Person = serde_json::from_str(&json_string)?;
println!("Deserialized: {:?}", deserialized);
// Pretty-print JSON
let pretty_json = serde_json::to_string_pretty(&person)?;
println!("Pretty: {}", pretty_json);
// Serialize complex nested structures
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Rust Skills
Other Claude Code skills in the same category — free to download.
Rust CLI
Build fast CLI applications with Clap in Rust
Rust API
Scaffold Rust web API with Actix-web or Axum
Rust Testing
Set up Rust unit and integration testing
Rust WASM
Build WebAssembly modules with Rust and wasm-pack
Rust Error Handling
Implement proper error handling with thiserror and anyhow
Rust Async
Implement async programming with Tokio runtime
Rust Error Handling Patterns
Build idiomatic error types using thiserror for libraries and anyhow for applications
Rust Async with Tokio
Write async Rust services with Tokio runtime for high-performance I/O
Want a Rust skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.