Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serde's Serialize implementation not found for Rocket's UUID

I'm trying to create a custom struct using the UUID struct from Rocket as a field type. I want it to be serialized using Serde in order to convert it to JSON easily.

When trying to do this, I get an error:

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`

My struct:

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

My imports:

[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"]

Endpoint where I use the struct:

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

I've checked all dependencies, and I have the serde feature enabled in rocket_contrib. I've run out of ideas what to check next.

like image 731
Mestru Avatar asked Oct 25 '18 14:10

Mestru


1 Answers

rocket_contrib::UUID does not implement Serialize:

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

If the type doesn't implement Serialize, you can't make it.

As mcarton points out:

you still can implement Serialize for your type, you simply can't derive it and will have to implement it by hand.

That could look something like:

#[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)
    }
}

See also:

  • How to transform fields during serialization using Serde?
  • How to transform fields during deserialization using Serde?
  • Is there a way for me to use #[derive] on a struct or enum from a library without editing the actual library's source code?
  • How do I implement a trait I don't own for a type I don't own?
  • Add Serialize attribute to type from third-party lib
like image 116
Shepmaster Avatar answered Nov 15 '22 08:11

Shepmaster