如何将 BSON 反序列化为通用对象?

How to deserialize BSON to a generic object?

我正在使用 Serde 将 BSON 对象反序列化为 Rust 结构实例。我可以将对象反序列化为具体的结构实例,但是我该如何反序列化呢?

我在 MongoDB 中有 "countries" 和 "cities" 集合。在 Rust 程序中,我有一个 CountryCity 的结构。当我从 Mongo 拉取一个国家或城市时,我可以使用 Serde 将其反序列化为 CountryCity 结构。请参阅下面 main() 中的第二行。

我想将 BSON 对象反序列化为通用 Location 对象。根据我在 Rust 书中读到的关于泛型的内容,我创建了一个特征 LocationTrait 并为 CountryCity 实现了它。 (参见 main() 中的第 3 行)。它无法编译,说 dyn LocationTrait 类型值的大小在编译时无法得知。

#[derive(Serialize, Deserialize)]
pub struct Country {
    pub name: String,
}

#[derive(Serialize, Deserialize)]
pub struct City {
    pub name: String,
}

pub trait LocationTrait {}
impl LocationTrait for Country {}
impl LocationTrait for City {}

fn main() {
    let item = mongo_coll
        .find_one(Some(doc! {"name": "usa"}), None)
        .unwrap()
        .unwrap();
    let country: Country = bson::from_bson(bson::Bson::Document(item)).unwrap();
    // fails -> let gen_location: LocationTrait = bson::from_bson(bson::Bson::Document(item)).unwrap();
}

最后,我想创建一个代表 CountryCity 的通用对象。但是,我不确定起点——我需要专注于特征还是需要创建一个新的特征绑定结构?

有两个问题阻止您的代码编译。

您看到的第一个错误:the size for values of type dyn LocationTrait cannot be known at compilation time,是由于 bson::from_bson 需要 return 按值反序列化的结果。编译器需要知道它需要在调用堆栈中分配多少 space 给 return 它。

然而,特征是描述行为而非数据的抽象,因此它可以实现为 u8(单个字节)或更大的结构。

为了能够 return 这样的值,您需要将其装箱(参见 Trait Objects)。

第二个问题是 return 值必须实现 Deserialize 特征(而不是 LocationTrait

要解决这些问题:

最简单的方法是使用枚举而不是特征:

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Location {
    Country(Country),
    City(City)
}

这将适用于 {"type" = "Country", name="usa"} 等文档。 检查 the Serde doc 以获得更多选项。

如果你真的想要使用特征(例如,为了能够在这个模块之外定义类型),你将需要一个盒装特征和一个自定义结构:

// The same trait as defined earlier
pub trait LocationTrait {}
impl LocationTrait for Country {}
impl LocationTrait for City {}

// A custom struct on which you can implement the deserialize trait
// Needed as both Deserialize and Box are defined outside this crate.
struct DynLocation(Box<dyn LocationTrait>);

impl<'de> Deserialize<'de> for DynLocation {
    fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // Tricky part ommited here:
        // You will need to partially deserialize you object
        // in order to get a first discriminant before instanciating
        // and deserializing the proper type.
        unimplemented!()
    }
}

// The public method to hide the DynLocation wrapper
pub fn deserialize(item: &str) -> Box<dyn LocationTrait> {
    let location: DynLocation = serde_json::from_str(item).expect("invalid json");
    location.0
}

可以在 中找到围绕同一主题的一些讨论。