Deserialize ObjectID


I didn't know why this code doesn't deserialise String to ObjectId.

use serde::{Deserialize, Deserializer};
use serde_json::Result;
use mongodb::bson::oid::ObjectId;

#[derive(Debug, Deserialize)]
struct Foo {
    foo: ObjectId,

fn main() {
    let p = r#"{"pid": "5a2e9fa42ccf470001da981c"}"#;
    println!("{:?}", serde_json::from_str(p));

This produce the following error:

Err(Error("invalid type: string "5a2e9fa42ccf470001da981c", expected expecting a Bson", line: 2, column: 37))

However ObjectId implements the Deserialize trait

How does it look if you serialize an ObjectId?

Serialize give:


Ok! so the mistake is the $oid (mongodb style)