Crates.io | serde-duration-ext |
lib.rs | serde-duration-ext |
version | 0.1.0 |
source | src |
created_at | 2024-02-09 08:28:53.987542 |
updated_at | 2024-02-09 08:28:53.987542 |
description | Serde support for std::time::Duration and chrono::Duration (chrono feature) |
homepage | |
repository | |
max_upload_size | |
id | 1133618 |
size | 20,076 |
Add the following to your Cargo.toml:
[dependencies]
serde_duration_ext = "0.1.0"
Also you can enable the chrono
feature to support chrono::Duration
[dependencies]
serde_duration_ext = { version = "0.1.0", features = ["chrono"] }
use serde::{Serialize, Deserialize};
use serde_json;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Foo {
#[serde(with = "serde_duration_ext")]
duration: std::time::Duration,
}
fn main() {
let foo = Foo {
duration: std::time::Duration::from_secs(123),
};
let json = serde_json::to_string(&foo).unwrap();
assert_eq!(json, r#"{"duration":"123s"}"#);
let foo2: Foo = serde_json::from_str(&json).unwrap();
assert_eq!(foo, foo2);
}
You can also use chrono::Duration
if you enable the chrono
feature.
use serde::{Serialize, Deserialize};
use serde_json;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Foo {
#[serde(with = "serde_duration_ext::chrono")]
duration: chrono::Duration,
}
fn main() {
let foo = Foo {
duration: chrono::Duration::seconds(123),
};
let json = serde_json::to_string(&foo).unwrap();
assert_eq!(json, r#"{"duration":"123s"}"#);
let foo2: Foo = serde_json::from_str(&json).unwrap();
assert_eq!(foo, foo2);
}
Library also provides useful types such as DurationUnit
and TimeUnit
use serde::{Serialize, Deserialize};
use serde_json;
use serde_duration_ext::DurationUnit;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Foo {
duration: DurationUnit,
}
fn main() {
let foo = Foo {
duration: DurationUnit::from_secs(123),
};
let json = serde_json::to_string(&foo).unwrap();
assert_eq!(json, r#"{"duration":"123s"}"#);
let foo2: Foo = serde_json::from_str(&json).unwrap();
assert_eq!(foo, foo2);
}