Closed Bryson14 closed 7 months ago
cargo pkgid mongodb
cargo pkgid bson
db.version()
A clear and concise description of what the bug is.
The Code I'm using to test this looks like this:
#[test] fn test_bson_datetime_parsing() { let dt = bson::DateTime::now(); let s = dt.to_string(); let iso = dt.try_to_rfc3339_string().unwrap(); let doc = doc!{ "time": dt }; let json = json!({"time": dt}); println!("df: {dt}, s: {s}, iso: {iso}, doc: {doc}, json: {json}"); let iso_string = "2024-04-05T19:24:00.408Z"; let json_str = format!( r#"{{ "time": {{"$date" : {{"$numberLong":"{}"}} }}, "id": "{}" }}"#, "1674504029491", "390f5f21-631a-463d-88a5-ebc29ca8a6e3" ); let parsed: BsonTester = serde_json::from_str(&json_str).unwrap(); println!("{}", parsed.time.unwrap().try_to_rfc3339_string().unwrap()) }
But the main issue is that I want the user to be able to see a json object like:
{ "time": "2024-04-05T19:24:00.408Z" }
And this could then be deserialized with serde_json automatically with a struct like this:
#[derive(Deserialize, Serialize, Debug)] struct BsonTester { time: Option<bson::DateTime>, id: Option<bson::Uuid> }
That way the client doesn have to deal with this mess of $date { $numberLong } when sending data to or from the web server
I think the bson_datetime_as_rfc3339_string helper will do exactly what you're looking for here 🙂
Versions/Environment
cargo pkgid mongodb
&cargo pkgid bson
)db.version()
)Describe the bug
A clear and concise description of what the bug is.
The Code I'm using to test this looks like this:
But the main issue is that I want the user to be able to see a json object like:
And this could then be deserialized with serde_json automatically with a struct like this:
That way the client doesn have to deal with this mess of $date { $numberLong } when sending data to or from the web server