Module unsigned_duration
Convenience routines for serializing std::time::Duration values.
The principal helpers in this module are the
required
and
optional sub-modules.
Either may be used with Serde's with attribute. Each sub-module
provides both a serialization and a deserialization routine for
std::time::Duration. Deserialization supports either ISO 8601 or the
"friendly" format. Serialization always uses ISO
8601 for reasons of increased interoperability. These helpers are meant to
approximate the Deserialize and Serialize trait implementations for
Jiff's own SignedDuration.
If you want to serialize a std::time::Duration using the
friendly, then you can make use of the
helpers in
friendly::compact,
also via Serde's with attribute. These helpers change their serialization
to the "friendly" format using compact unit designators. Their deserialization
remains the same as the top-level helpers (that is, both ISO 8601 and
friendly formatted duration strings are parsed).
Unlike Jiff's own SignedDuration, deserializing
a std::time::Duration does not support negative durations. If a negative
duration is found, then deserialization will fail. Moreover, as an unsigned
type, a std::time::Duration can represent larger durations than a
SignedDuration. This means that a SignedDuration cannot deserialize
all valid values of a std::time::Duration. In other words, be careful not
to mix them.
Example: maximally interoperable serialization
This example shows how to achieve Serde integration for std::time::Duration
in a way that mirrors SignedDuration. In
particular, this supports deserializing ISO 8601 or "friendly" format
duration strings. In order to be maximally interoperable, this serializes
only in the ISO 8601 format.
use Duration;
use ;
let task = Task ;
let expected_json = r#"{"name":"Task 1","timeout":"PT1H30M","retry_delay":"PT2.5S"}"#;
let actual_json = to_string?;
assert_eq!;
let deserialized_task: Task = from_str?;
assert_eq!;
// Example with None for optional field
let task_no_retry = Task ;
let expected_json_no_retry = r#"{"name":"Task 2","timeout":"PT5S","retry_delay":null}"#;
let actual_json_no_retry = to_string?;
assert_eq!;
let deserialized_task_no_retry: Task = from_str?;
assert_eq!;
# Ok::
Example: Round-tripping std::time::Duration
This example demonstrates how to serialize and deserialize a
std::time::Duration field using the helpers from this module. In
particular, this serializes durations in the more human readable
"friendly" format, but can still deserialize ISO 8601 duration strings.
use Duration;
use ;
let task = Task ;
let expected_json = r#"{"name":"Task 1","timeout":"1h 30m","retry_delay":"2s 500ms"}"#;
let actual_json = to_string?;
assert_eq!;
let deserialized_task: Task = from_str?;
assert_eq!;
// Example with None for optional field
let task_no_retry = Task ;
let expected_json_no_retry = r#"{"name":"Task 2","timeout":"5s","retry_delay":null}"#;
let actual_json_no_retry = to_string?;
assert_eq!;
let deserialized_task_no_retry: Task = from_str?;
assert_eq!;
# Ok::
Example: custom "friendly" format options
When using
friendly::compact,
the serialization implementation uses a fixed friendly format
configuration. To use your own configuration, you'll need to write your own
serialization function:
use Duration;
let json = r#"{"duration": "36 hours 1100ms"}"#;
let got: Data = from_str.unwrap;
assert_eq!;
let expected = r#"{"duration":"36:00:01.100"}"#;
assert_eq!;
Modules
-
friendly
(De)serialize a
std::time::Durationin thefriendlyduration format. -
optional
(De)serialize an optional ISO 8601 or friendly duration from a
std::time::Duration. -
required
(De)serialize a required ISO 8601 or friendly duration from a
std::time::Duration.