-
Notifications
You must be signed in to change notification settings - Fork 137
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
add ObjectId deserialization from sequence to support messagepack #457
Comments
Hey @univerz, thanks for opening this issue! We likely don't want to add the visit method you're suggesting because it makes assumptions about the shape of an |
well, it has to, because
it would conflict using the same struct with mongodb. |
It's true that If you need to support dual-functionality for deserialization, I recommend writing a function to use with the
|
There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week. |
i noticed that bson still uses inefficient however, the deserialization path would need more thought, so i decided to just make
diff --git a/src/extjson/models.rs b/src/extjson/models.rs
index efb57ff..b31b4eb 100644
--- a/src/extjson/models.rs
+++ b/src/extjson/models.rs
@@ -93,6 +93,7 @@ impl Decimal128 {
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
+#[serde(rename = "$oid")]
pub(crate) struct ObjectId {
#[serde(rename = "$oid")]
oid: String,
diff --git a/src/raw/bson_ref.rs b/src/raw/bson_ref.rs
index 8241d87..3986615 100644
--- a/src/raw/bson_ref.rs
+++ b/src/raw/bson_ref.rs
@@ -326,7 +326,9 @@ impl<'a> Serialize for RawBsonRef<'a> {
RawBsonRef::Null => serializer.serialize_unit(),
RawBsonRef::Int32(v) => serializer.serialize_i32(*v),
RawBsonRef::Int64(v) => serializer.serialize_i64(*v),
- RawBsonRef::ObjectId(oid) => oid.serialize(serializer),
+ RawBsonRef::ObjectId(oid) => {
+ crate::extjson::models::ObjectId::from(*oid).serialize(serializer)
+ }
RawBsonRef::DateTime(dt) => dt.serialize(serializer),
RawBsonRef::Binary(b) => b.serialize(serializer),
RawBsonRef::JavaScriptCode(c) => {
@@ -678,13 +680,13 @@ impl<'a> Serialize for RawDbPointerRef<'a> {
ref_ns: &'a str,
#[serde(rename = "$id")]
- id: ObjectId,
+ id: crate::extjson::models::ObjectId,
}
let mut state = serializer.serialize_struct("$dbPointer", 1)?;
let body = BorrowedDbPointerBody {
ref_ns: self.namespace,
- id: self.id,
+ id: self.id.into(),
};
state.serialize_field("$dbPointer", &body)?;
state.end()
diff --git a/src/ser/serde.rs b/src/ser/serde.rs
index e2866a5..7990a49 100644
--- a/src/ser/serde.rs
+++ b/src/ser/serde.rs
@@ -32,9 +32,11 @@ impl Serialize for ObjectId {
where
S: serde::ser::Serializer,
{
- let mut ser = serializer.serialize_struct("$oid", 1)?;
- ser.serialize_field("$oid", &self.to_string())?;
- ser.end()
+ if !serializer.is_human_readable() {
+ serializer.serialize_bytes(&self.bytes())
+ } else {
+ crate::extjson::models::ObjectId::from(*self).serialize(serializer)
+ }
}
}
@@ -67,7 +69,9 @@ impl Serialize for Bson {
Bson::Null => serializer.serialize_unit(),
Bson::Int32(v) => serializer.serialize_i32(*v),
Bson::Int64(v) => serializer.serialize_i64(*v),
- Bson::ObjectId(oid) => oid.serialize(serializer),
+ Bson::ObjectId(oid) => {
+ crate::extjson::models::ObjectId::from(*oid).serialize(serializer)
+ }
Bson::DateTime(dt) => dt.serialize(serializer),
Bson::Binary(b) => b.serialize(serializer),
Bson::JavaScriptCode(c) => {
diff --git a/src/serde_helpers.rs b/src/serde_helpers.rs
index 42c1657..8de4a99 100644
--- a/src/serde_helpers.rs
+++ b/src/serde_helpers.rs
@@ -408,7 +408,7 @@ pub mod hex_string_as_object_id {
/// Serializes a hex string as an ObjectId.
pub fn serialize<S: Serializer>(val: &str, serializer: S) -> Result<S::Ok, S::Error> {
match ObjectId::parse_str(val) {
- Ok(oid) => oid.serialize(serializer),
+ Ok(oid) => crate::extjson::models::ObjectId::from(oid).serialize(serializer),
Err(_) => Err(ser::Error::custom(format!(
"cannot convert {} to ObjectId",
val |
now i see why it is flawed. |
maybe the |
I filed RUST-1886 to consider using the newtype path for ObjectIDs. The team has discussed doing some broader cleanup work on our serde implementations (which would likely include RUST-426), so we'll look more into this when we prioritize that project. I'm going to close this out, but please let us know if you have any further questions or suggestions! |
i ran into this problem again when using |
de/serializing
ObjectId
works when usingrmp_serde::encode::to_vec_named
, but not withrmp_serde::encode::to_vec
which stores object as an array without field keys. example how it looks like:output
because the
ObjectId
behavior is special, i (probably) need something like this added toObjectIdVisitor
to make it work (serialize_object_id_as_hex_string
is not an option):are you open to it, or do you see a better solution?
The text was updated successfully, but these errors were encountered: