Skip to content

RUST-1178 The ability to custom tag DateTime<Utc> for automatic conversion. #191

Open
@ruseinov

Description

@ruseinov

Here's an example of a collection with records expiring by expires_at:

#[collection = "subscriptions"]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Subscription {
    #[serde(rename = "_id")]
    pub id: Option<ObjectId>,
    pub expires_at: DateTime<Utc>,
}

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

    "expires_at": {
        "$date": {
            "$numberLong": 1591700287095
        }
    },

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    tracked-in-jiraTicket filed in Mongo's Jira system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions