Skip to content

Commit 343a7ce

Browse files
RUST-1933: Support deserializing $uuid extended JSON syntax (#474)
1 parent 8ecf27a commit 343a7ce

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Diff for: src/de/serde.rs

+8
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ impl<'de> Visitor<'de> for BsonVisitor {
381381
));
382382
}
383383

384+
"$uuid" => {
385+
let v: String = visitor.next_value()?;
386+
let uuid = extjson::models::Uuid { value: v }
387+
.parse()
388+
.map_err(Error::custom)?;
389+
return Ok(Bson::Binary(uuid));
390+
}
391+
384392
"$code" => {
385393
let code = visitor.next_value::<String>()?;
386394
if let Some(key) = visitor.next_key::<String>()? {

Diff for: src/extjson/models.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl Binary {
188188
#[serde(deny_unknown_fields)]
189189
pub(crate) struct Uuid {
190190
#[serde(rename = "$uuid")]
191-
value: String,
191+
pub(crate) value: String,
192192
}
193193

194194
impl Uuid {

Diff for: src/tests/serde.rs

+15
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,21 @@ fn test_serde_legacy_uuid_1() {
650650
assert_eq!(foo.csharp_legacy, uuid);
651651
}
652652

653+
#[test]
654+
fn test_de_uuid_extjson_string() {
655+
let _guard = LOCK.run_concurrently();
656+
657+
let uuid_bson_bytes =
658+
hex::decode("1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400").unwrap();
659+
let uuid_document = Document::from_reader(uuid_bson_bytes.as_slice()).unwrap();
660+
let expected_uuid_bson = Bson::from_extended_document(uuid_document);
661+
662+
let ext_json_uuid = "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}";
663+
let actual_uuid_bson: Bson = serde_json::from_str(ext_json_uuid).unwrap();
664+
665+
assert_eq!(actual_uuid_bson, expected_uuid_bson);
666+
}
667+
653668
#[test]
654669
fn test_de_oid_string() {
655670
let _guard = LOCK.run_concurrently();

0 commit comments

Comments
 (0)