Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fmt: add new
serde
sub-module for integer timestamp integration
This adds a new `jiff::fmt::serde` sub-module that contains helper sub-modules that work with Serde's `with` attribute. For example: ```rust use jiff::Timestamp; struct Record { #[serde(with = "jiff::fmt::serde::timestamp::second::required")] timestamp: Timestamp, } let json = r#"{"timestamp":1517644800}"#; let got: Record = serde_json::from_str(&json)?; assert_eq!(got.timestamp, Timestamp::from_second(1517644800)?); assert_eq!(serde_json::to_string(&got)?, json); ``` This is inspired in part by how Chrono supports a similar use case. It is expected that the behavior should be the same, although this implementation does support the full gamut of integer types (including a 128-bit integer number of nanoseconds). Moreover, the naming is different. Chrono uses a flatter namespace, where as here, we bury everything into sub-modules. The idea is to leave some room for future expansion, although I'm not sure there is much else to add. I also feel like spelling out `timestamp` instead of `ts` is a bit clearer. The module paths are quite long, e.g., `jiff::fmt::serde::timestamp::second::required` and `jiff::fmt::serde::timestamp::second::optional`. But they are predictable. And to mitigate users needing to click around through a deep module tree, we include the full tree in the `jiff::fmt::serde` module documentation. Ref #100, Closes #101
- Loading branch information