rust - 将默认字符串值添加到反序列化的 serde 结构失败,特征 Deserialize<'_> 未实现

标签 rust serde

我有以下从 XML 文件反序列化的结构。

#[derive(Deserialize, Debug)]
pub struct RawItem {
    id: String,
    title: String,
    description: Option<String>,
    link: String,
    image_link: String,
    availability: String,
    price: String,
}

到目前为止一切顺利,对象已正确反序列化。现在,我的 XML 数据略有变化,价格字段变为可选。我想在丢失时将价格回退到“0.0”,因此我使用了 serde 默认机制:

#[derive(Deserialize, Debug)]
pub struct RawItem {
    id: String,
    title: String,
    description: Option<String>,
    link: String,
    image_link: String,
    availability: String,
    #[serde(default = "0.0")] // Added this new line
    price: String,
}

由于某种原因,它失败并出现以下错误:

error: failed to parse path: "0.0"
  --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:81:20
   |
81 |     #[serde(default = "0.0")]
   |                       ^^^^^

error[E0277]: the trait bound `xxxx_specifics::RawItem: Deserialize<'_>` is not satisfied
    --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:68:2
     |
68   |     #[serde(rename = "item")]
     |     ^ the trait `Deserialize<'_>` is not implemented for `xxxx_specifics::RawItem`
     |
     = note: required because of the requirements on the impl of `Deserialize<'_>` for `Vec<xxxx_specifics::RawItem>`
note: required by `next_element`
    --> /home/xxxxx/serde-1.0.130/src/de/mod.rs:1703:5
     |
1703 | /     fn next_element<T>(&mut self) -> Result<Option<T>, Self::Error>
1704 | |     where
1705 | |         T: Deserialize<'de>,
     | |____________________________^

error[E0277]: the trait bound `xxxx_specifics::RawItem: Deserialize<'_>` is not satisfied
    --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:68:2
     |
68   |     #[serde(rename = "item")]
     |     ^ the trait `Deserialize<'_>` is not implemented for `xxxx_specifics::RawItem`
     |
     = note: required because of the requirements on the impl of `Deserialize<'_>` for `Vec<xxxx_specifics::RawItem>`
note: required by `next_value`
    --> /home/xxxx/serde-1.0.130/src/de/mod.rs:1842:5
     |
1842 | /     fn next_value<V>(&mut self) -> Result<V, Self::Error>
1843 | |     where
1844 | |         V: Deserialize<'de>,
     | |____________________________^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `inko_importer` due to 3 previous errors

serde 默认值的任何原因都会因 String 上的简单默认值而失败?

最佳答案

default属性期望调用一个方法(实际上是一个带有方法路径的字符串):

use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct RawItem {
    #[serde(default = "default_0")] // Added this new line
    price: String
}

fn default_0() -> String {
    "0.0".to_string()
}

Playground

关于rust - 将默认字符串值添加到反序列化的 serde 结构失败,特征 Deserialize<'_> 未实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70077647/

相关文章:

struct - 如何根据键名反序列化为枚举变体?

rust - FFI:将可空指针转换为选项

rust - 为什么分配时会得到 "temporary value dropped while borrowed",但通过函数传递时却不会?

rust - 为什么 Rust 不能识别变量是 &str?

json - 如何在 Serde 中(反)序列化强类型的 JSON 字典?

sockets - Rust 中的套接字服务器不接收

json - 使用 serde_json 解析对象内部的对象

rust - 使用Rust中的Serde从json提升嵌套值可能是可选的

xml - 解析文件返回错误 "thread ' main' paniced"

rust - 如何从字符串或数字反序列化无字段枚举?