Skip to content

Commit 8cf62fc

Browse files
authored
feat(extjson): parse legacy {"$date": <ms since epoch>} (#519)
1 parent f2cbaf1 commit 8cf62fc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/extjson/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ pub(crate) struct DateTime {
256256
pub(crate) enum DateTimeBody {
257257
Canonical(Int64),
258258
Relaxed(String),
259+
Legacy(i64),
259260
}
260261

261262
impl DateTimeBody {
@@ -282,6 +283,7 @@ impl DateTime {
282283
})?;
283284
Ok(datetime)
284285
}
286+
DateTimeBody::Legacy(ms) => Ok(crate::DateTime::from_millis(ms)),
285287
}
286288
}
287289
}

src/tests/serde.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,19 @@ fn test_de_uuid_extjson_string() {
665665
assert_eq!(actual_uuid_bson, expected_uuid_bson);
666666
}
667667

668+
#[test]
669+
fn test_de_date_extjson_number() {
670+
let _guard = LOCK.run_concurrently();
671+
672+
let ext_json_canonical = r#"{ "$date": { "$numberLong": "1136239445000" } }"#;
673+
let expected_date_bson: Bson = serde_json::from_str(ext_json_canonical).unwrap();
674+
675+
let ext_json_legacy_java = r#"{ "$date": 1136239445000 }"#;
676+
let actual_date_bson: Bson = serde_json::from_str(ext_json_legacy_java).unwrap();
677+
678+
assert_eq!(actual_date_bson, expected_date_bson);
679+
}
680+
668681
#[test]
669682
fn test_de_oid_string() {
670683
let _guard = LOCK.run_concurrently();

0 commit comments

Comments
 (0)