Skip to main content

Module option

Module option 

Source
Expand description

The same format for an optional date-time (Option<DateTime<Utc>>).

Reading is more forgiving than deserialize: null, an empty text and the epoch text 1970-01-01T00:00:00.000Z all read as None. The broker uses the epoch as “no time” in some order fields. Writing None gives null. Use it with #[serde(default, with = "...option")].

§Examples

use brokapi_bcs_protocol::utils::date_format;
use chrono::{DateTime, Utc};
use serde::Deserialize;

#[derive(Deserialize)]
struct Event {
    #[serde(default, with = "date_format::option")]
    at: Option<DateTime<Utc>>,
}

let set: Event = serde_json::from_str(r#"{"at":"2026-10-19T07:05:00.000Z"}"#)?;
assert!(set.at.is_some());
let epoch: Event = serde_json::from_str(r#"{"at":"1970-01-01T00:00:00.000Z"}"#)?;
assert!(epoch.at.is_none());
let missing: Event = serde_json::from_str("{}")?;
assert!(missing.at.is_none());

Functions§

deserialize
Reads null, an empty text and the epoch text as None, and any other date-time as Some.
serialize
Writes Some as in super::serialize and None as null.