How to handle dates with serde

Add the chrono crate with serde features and derive Serialize/Deserialize on DateTime fields to handle dates.

Use the chrono crate with the serde feature to serialize and deserialize dates automatically. Add chrono = { version = "0.4", features = ["serde"] } to your Cargo.toml, then derive Serialize and Deserialize on your struct fields using chrono::DateTime<Utc>.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Event {
    timestamp: DateTime<Utc>,
}

This handles ISO 8601 formatting by default when converting to or from JSON.