将默认字符串值添加到反序列化的 serde 结构失败,特征 Deserialize<'_> 未实现
Adding a default String value to a deserialized serde struct fails with trait Deserialize<'_> not implemented
我有以下从 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 默认值的任何原因都会因字符串上的简单默认值而失败?
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()
}
我有以下从 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 默认值的任何原因都会因字符串上的简单默认值而失败?
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()
}