Cannot serialize BigDecimal and Duration

I am trying to Serialize a struct using serde and running into issues with BigDecimal and Duration types.

use bigdecimal::BigDecimal;
use chrono::Duration;
use serde::{Serialize};

pub struct MyStruct {
    pub a: Duration,
    pub b: BigDecimal,

The error I get is because Serialize trait is not implemented for these types.

the trait bound `chrono::Duration: _serde::Serialize` is not satisfied
the trait `_serde::Serialize` is not implemented for `chrono::Duration`

Could you suggest how to proceed?

If you check the documentation of bigdecimal, there's a serde feature flag. This is conventional.

As for chrono, there's no such feature flag. You have to decide how you want to serialize the duration (e.g. convert it to a string in ISO-8601 format), and then you can write a function to use with the #[serde(with = …)] attribute.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.