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

Introduce alternative serialization and deserialization "strategies"

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: bson-1.2.0
    • Component/s: None
    • Labels:

      Description

      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.

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: