Skip to content

RUST-1540 Add Ability to deserialize String to ObjectId and i64 to DateTime #381

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

Closed
borngraced opened this issue Oct 31, 2022 · 1 comment · Fixed by #383
Closed

RUST-1540 Add Ability to deserialize String to ObjectId and i64 to DateTime #381

borngraced opened this issue Oct 31, 2022 · 1 comment · Fixed by #383
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@borngraced
Copy link
Contributor

borngraced commented Oct 31, 2022

For some reasons I see Bson doesn't deserialize Objected and DateTime to String and i64 respectively, so I think it'd be very helpful if there's an inbuilt helper fn to use with serde's deserialize_with macro attribute to automatically deserialize ObjectId to String and DateTime to i64..

Example

fn des_objectid<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: de::Deserializer<'de>,
{
    let s: ObjectId = de::Deserialize::deserialize(deserializer)?;

    Ok(s.to_string())
}

then we'll be able to do something like this

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    #[serde(deserialize_with = "des_objectid")]
    pub _id: String,
    }

This is very useful for returning ObjectId as String and DateTime as i64

{ "id": "3895893bf983nc9..." }

instead of something like this

{ "id": { "$oid" : "3895893bf983nc9..."} }
@isabelatkinson
Copy link
Contributor

Hi @borngraced, thanks for filling this issue! We have a serde_helpers module that contains functions you can use with the serialize_with and deserialize_with attributes. Included is a hex_string_as_object_id that allows conversion between ObjectIds and strings. We don't, however, have helpers for converting between DateTimes and i64s, so I've filed RUST-1540 to add them. If you're interesting in contributing to the BSON library, feel free to make a pull request implementing this functionality.

@bajanam bajanam added waiting-for-reporter tracked-in-jira Ticket filed in Mongo's Jira system and removed waiting-for-reporter labels Nov 8, 2022
@bajanam bajanam changed the title [FR] Add Ability to deserialize String to ObjectId and i64 to DateTime RUST-1540 [FR] Add Ability to deserialize String to ObjectId and i64 to DateTime Nov 8, 2022
@bajanam bajanam changed the title RUST-1540 [FR] Add Ability to deserialize String to ObjectId and i64 to DateTime RUST-1540 Add Ability to deserialize String to ObjectId and i64 to DateTime Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants