Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

well-known serde for Vec<OffsetDateTime> #452

Open
kamulos opened this issue Mar 8, 2022 · 3 comments
Open

well-known serde for Vec<OffsetDateTime> #452

kamulos opened this issue Mar 8, 2022 · 3 comments
Labels
A-well-known-format-description Area: well known format descriptions C-feature-request Category: a new feature (not already implemented)

Comments

@kamulos
Copy link

kamulos commented Mar 8, 2022

I am porting my program from chrono to time. One thing I am stuck with is serializing a Vec<OffsetDateTime> with the rfc3339 format.

This seems like a small but very useful addition. Is this something that would be considered to be included?

@kamulos
Copy link
Author

kamulos commented Mar 8, 2022

This is what just tried to use in the serialize_with attribute:

fn date_vec_ser<S: Serializer>(
    vec: &[OffsetDateTime],
    serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
    let stringvec = vec
        .iter()
        .map(|odt| odt.format(&Rfc3339))
        .collect::<std::result::Result<Vec<String>, time::error::Format>>()
        .map_err(S::Error::custom)?;
    stringvec.serialize(serializer)
}

@jhpratt jhpratt added A-well-known-format-description Area: well known format descriptions C-feature-request Category: a new feature (not already implemented) labels Mar 8, 2022
@jhpratt
Copy link
Member

jhpratt commented Mar 8, 2022

I'm not going to say it's 100%, but I think this is reasonable.

@jonasbb
Copy link

jonasbb commented Jul 20, 2022

There is support for the well-known formats in serde_with, such that you can write this code and support other nestings.

#[serde_with::serde_as]
#[derive(Debug, serde::Deserialize)]
struct Data(
    #[serde_as(as = "Vec<time::format_description::well_known::Rfc3339>")]
    Vec<time::OffsetDateTime>
);

let v = serde_json::json!([
    "1997-11-21T01:55:06-06:00",
    "2000-01-21T05:55:06-06:00",
    "2020-03-21T09:55:06-06:00",
    "1997-11-21T23:55:06-06:00",
]);
let d: Data = serde_json::from_value(v).unwrap();
dbg!(d);

https://www.rustexplorer.com/b/kdutp7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-well-known-format-description Area: well known format descriptions C-feature-request Category: a new feature (not already implemented)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants