Skip to content

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

@rolandog-kb4

Description

@rolandog-kb4

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions