-
Notifications
You must be signed in to change notification settings - Fork 134
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
RUST-1178 The ability to custom tag DateTime<Utc> for automatic conversion. #191
Comments
Here's my temp fix, which is verbose, but it does the job. pub struct SubscriptionDTO {
pub data: Subscription,
}
impl Serialize for SubscriptionDTO {
fn serialize<S>(
&self,
serializer: S,
) -> result::Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("SubscriptionDTO", 5)?;
... other fields
state.serialize_field("expires_at", &self.data.expires_at.0)?;
state.end()
}
} |
To reduce the amount of code required to achieve this, you could use I filed RUST-506 to track the status of work on this. Thank you for filing the issue! |
Any updates on this? Also why is the jira issue closed? |
In RUST-506 we introduced some more general purpose helpers, but as you point out they didn't quite solve the initial problem of automatic serialization changes on a per-format basis. For that, I've filed RUST-1178. Thanks for bringing this to our attention! Until that ticket gets completed, you can use separate structs for communicating with the DB and with the frontend which have different serialization implementations. |
@ruseinov , have you found any other solutions to this problem except an extra wrapper struct? |
@max-block TBH I don't remember anymore, but I think not. Haven't been interfacing with MongoDB lately, therefore I have got no recent experience with BSON. |
Any updates on this? I ended up with this. When I pass this into my database create function, the create function converts it into a struct Foobar<T = DateTime<Utc>> {
created_date: T,
}
impl From<Foobar<DateTime<Utc>>> for Foobar<bson::DateTime> {
fn from(value: Foobar<DateTime<Utc>>) -> Self {
Foobar {
created_date: value.created_date.into(),
}
}
} |
Hi @clarkmcc, while our current development plan is at capacity, this remains in our backlog for future prioritization. Thank you for the suggestion! |
Any news here? |
Here's an example of a collection with records expiring by
expires_at
:In order for that to work I need to use the Bson::UtcDateTime helper (now DateTime), but in that case when I deliver this model to the frontend I end up with
instead of proper date. The approach with duplicating the object just for the sake date serialization is not the most convenient one.
What would be nice to have is some sort of annotation that would tell Bson Encoder/Decoder that this DateTime field has to be de/serialized as Bson date, while serde_json will serialize it as usual.
The same applies to ObjectId, currently if one wants a readable hex string instead of a json object there's a lot of duplication involved.
The text was updated successfully, but these errors were encountered: