Skip to content

how do I (de)serialize an integer number of seconds since the Unix epoch? #100

Closed Answered by BurntSushi
dsully asked this question in Q&A
Discussion options

You must be logged in to vote

All righty, jiff 0.1.11 has a much better answer for this:

use jiff::Timestamp;

struct Record {
    #[serde(with = "jiff::fmt::serde::timestamp::second::optional")]
    timestamp: Option<Timestamp>,
}

let json = r#"{"timestamp":1517644800}"#;
let got: Record = serde_json::from_str(&json)?;
assert_eq!(got.timestamp, Some(Timestamp::from_second(1517644800)?));
assert_eq!(serde_json::to_string(&got)?, json);

For other examples and docs, see: https://docs.rs/jiff/0.1.11/jiff/fmt/serde/index.html

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@dsully
Comment options

@BurntSushi
Comment options

@BurntSushi
Comment options

@dsully
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #99 on August 15, 2024 20:51.