cargoserde95% confidence\u2191 8

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

Full error message
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.

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

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/4c9a474dff896602f924cb0df102499ca3cc7803bc44815ff4d7963e2fb02a3e
hash \u00b7 4c9a474dff896602f924cb0df102499ca3cc7803bc44815ff4d7963e2fb02a3e
Serde&#39;s Serialize implementation not found for Rocket&#3… — DepScope fix | DepScope