6

我正在尝试使用UUIDRocket 中的结构作为字段类型来创建自定义结构。我希望使用 Serde 对其进行序列化,以便轻松将其转换为 JSON。

尝试执行此操作时,出现错误:

error[E0277]: the trait bound `rocket_contrib::UUID: 
model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Serialize` is not 
satisfied
 --> src/service/document.rs:4:10
  |
4 | #[derive(Serialize, Deserialize)]
  |          ^^^^^^^^^ the trait 
`model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Serialize` is not 
implemented for `rocket_contrib::UUID`
  |
  = note: required by `model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `rocket_contrib::UUID: 
model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Deserialize<'_>` is not satisfied
 --> src/service/document.rs:4:21
  |
4 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^ the trait 
`model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Deserialize<'_>` is not implemented for `rocket_contrib::UUID`
  |
  = note: required by `model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `rocket_contrib::UUID: model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Deserialize<'_>` is not satisfied
 --> src/service/document.rs:4:21
  |
4 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^ the trait `model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Deserialize<'_>` is not implemented for `rocket_contrib::UUID`
  |
  = note: required by `model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::de::MapAccess::next_value`

我的结构:

#[derive(Serialize, Deserialize)]
pub struct Document {
    id: UUID,
    user_id: UUID,
    created: i64,
    updated: i64,
    text: String
}

我的进口:

[dependencies]
rocket = "0.3.17"
rocket_codegen = "0.3.17"

serde_derive = "1.0.80"
serde = "1.0.80"

chrono = "0.4"

[dependencies.rocket_contrib]
version = "0.3.17"
default-features = false
features = ["json", "uuid", "serde"]

我使用结构的端点:

#[get("/document/<id>")]
pub fn get_document(id: UUID) -> status::Accepted<Json<Document>> {
    status::Accepted(Some(Json(document::get_document(id))))
}

我检查了所有依赖项,并且serderocket_contrib. 我已经没有想法接下来要检查什么了。

4

1 回答 1

7

rocket_contrib::UUID实施Serialize

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct UUID(uuid_ext::Uuid);

如果类型未实现Serialize,则无法实现。

正如mcarton 指出的那样

您仍然可以为您的类型实现 Serialize,您根本无法派生它并且必须手动实现它。

这可能看起来像:

#[derive(Serialize, Deserialize)]
pub struct Document {
    #[serde(with = "my_uuid")]
    id: UUID,
    #[serde(with = "my_uuid")]
    user_id: UUID,
    created: i64,
    updated: i64,
    text: String,
}

mod my_uuid {
    use rocket_contrib::UUID;
    use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
    use std::str::FromStr;

    pub fn serialize<S>(val: &UUID, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        val.to_string().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<UUID, D::Error>
    where
        D: Deserializer<'de>,
    {
        let val: &str = Deserialize::deserialize(deserializer)?;
        UUID::from_str(val).map_err(D::Error::custom)
    }
}

也可以看看:

于 2018-10-25T15:07:47.840 回答