Uploaded image for project: 'Rust Driver'
  1. Rust Driver
  2. RUST-506

Introduce alternative serialization and deserialization "strategies"

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-1.2.0
    • Affects Version/s: None
    • Component/s: None
    • None

      Currently, the BSON types generic Deserialize and Serialize implementations ingest and produce extended JSON-like format when used with non Bson serializers / deserializers. We should consider defining a few helper functions to enable (de)serializing different representations, like a hex string for ObjectId or the default DateTime serialization.

      Issue originally file by ruseinov on GitHub (https://github.com/mongodb/bson-rust/issues/191):

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

      #[collection = "subscriptions"]
      #[derive(Serialize, Deserialize, Debug, Clone)]
      pub struct Subscription

      Unknown macro: { #[serde(rename = "_id")] pub id}

      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":

      Unknown macro: { "$numberLong"}

      },
      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.

            Assignee:
            isabel.atkinson@mongodb.com Isabel Atkinson
            Reporter:
            patrick.freed@mongodb.com Patrick Freed
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: