Feature Request: Support Option<Uuid> in uuid::serde::simple module #840

Open
opened 2025-08-22 11:06:01 -05:00 by rolandog-kb4 · 1 comment
rolandog-kb4 commented 2025-08-22 11:06:01 -05:00 (Migrated from github.com)

Summary
Add support for serializing and deserializing Option types using the simple UUID format in the uuid::serde::simple module.

Problem
Currently, the uuid::serde::simple module only supports direct Uuid types, but not Option. When attempting to use serde(with = "uuid::serde::simple") on an Option field, the following compilation error occurs:

error[E0308]: mismatched types
serde(with = "uuid::serde::simple", default)
   |                      ^^^^^^^^^^^^^^^^^^^^^ expected `Option<Uuid>`, found `Uuid`
   |
   = note: `?` operator cannot convert from `uuid::Uuid` to `std::option::Option<uuid::Uuid>`
   = note: expected enum `std::option::Option<uuid::Uuid>`
            found struct `uuid::Uuid`

Current Workaround
Need to implement custom serialization/deserialization functions.

pub fn serialize_optional_uuid<S>(
        value: &Option<Uuid>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match value {
            Some(uuid) => uuid::serde::simple::serialize(uuid, serializer),
            None => serializer.serialize_none(),
        }
    }

    pub fn deserialize_optional_uuid<'de, D>(deserializer: D) -> Result<Option<Uuid>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let opt: Option<String> = Option::deserialize(deserializer)?;
        match opt {
            Some(s) => Ok(Some(uuid::serde::simple::deserialize(
                serde::de::value::StrDeserializer::new(&s),
            )?)),
            None => Ok(None),
        }
    }

It would be nice if you can do:
uuid::serde::simple::option

**Summary** Add support for serializing and deserializing Option<Uuid> types using the simple UUID format in the uuid::serde::simple module. **Problem** Currently, the uuid::serde::simple module only supports direct Uuid types, but not Option<Uuid>. When attempting to use serde(with = "uuid::serde::simple") on an Option<Uuid> field, the following compilation error occurs: ``` error[E0308]: mismatched types serde(with = "uuid::serde::simple", default) | ^^^^^^^^^^^^^^^^^^^^^ expected `Option<Uuid>`, found `Uuid` | = note: `?` operator cannot convert from `uuid::Uuid` to `std::option::Option<uuid::Uuid>` = note: expected enum `std::option::Option<uuid::Uuid>` found struct `uuid::Uuid` ``` **Current Workaround** Need to implement custom serialization/deserialization functions. ``` pub fn serialize_optional_uuid<S>( value: &Option<Uuid>, serializer: S, ) -> Result<S::Ok, S::Error> where S: Serializer, { match value { Some(uuid) => uuid::serde::simple::serialize(uuid, serializer), None => serializer.serialize_none(), } } pub fn deserialize_optional_uuid<'de, D>(deserializer: D) -> Result<Option<Uuid>, D::Error> where D: Deserializer<'de>, { let opt: Option<String> = Option::deserialize(deserializer)?; match opt { Some(s) => Ok(Some(uuid::serde::simple::deserialize( serde::de::value::StrDeserializer::new(&s), )?)), None => Ok(None), } } ``` It would be nice if you can do: `uuid::serde::simple::option`
KodrAus commented 2026-02-06 17:31:25 -06:00 (Migrated from github.com)

Thanks for the suggestion @rolandog-kb4. This seems like something that might be better supported by composing serde attributes, but there isn't one for optional values. Looking at our implementation, I think adding an option submodule of each of the existing simple, hyphenated, urn, etc. that accepted Option<Uuid> and forwarded to their parent or serialize_none() wouldn't result in a lot more code for us to maintain.

Thanks for the suggestion @rolandog-kb4. This seems like something that might be better supported by composing `serde` attributes, but there isn't one for optional values. Looking at [our implementation](https://github.com/uuid-rs/uuid/blob/main/src/external/serde_support.rs), I think adding an `option` submodule of each of the existing `simple`, `hyphenated`, `urn`, etc. that accepted `Option<Uuid>` and forwarded to their parent or `serialize_none()` wouldn't result in a lot more code for us to maintain.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-uuid-rs/uuid#840
No description provided.