You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add optional support for serde serialization & de-serialization #533
When using lofty in my project that uses axum, I noticed I couldn't serialize TagType, which isn't a big deal as I can just recreate the enum with serde::Serialize and serde::Deserialize attached to it.
However it would nice to have optional support for serde via an optional feature.
I don't think that this would be suitable. As long as there is no canonical, serialized representation for those types it should not be implemented in lofty.
If I'm misunderstanding something, please let me know.
Again if this feature can/should not be added, it's not a huge deal, all I have to do is create duplicate types in my project that derives from serde::Serialize and serde::Deserialize like so:
I could see the value in having the basic *Type enums (FileType, TagType, PictureType, MimeType) support serialization for use in a database. They'd all just be basic strings anyway. I'd avoid something like ItemKey, though.
I don't believe I fully understand why this wouldn't be suitable.
Not everything that is technically possible or easy is sensible.
IMHO it is out of scope of this library and would add an unnecessary maintenance burden.
Even for simple strings you would need to make an opinionated decision which formatting to use (camelCase, PascalCase, ...). This might be inconsistent with how enumerations are supposed to be serialized in the outer/enclosing context. It is unlikely that the lofty types are used in isolation.
In non-trivial projects you should always keep the serialization or schema separate from the Domain Model. APIs and persistence have different requirements for serialization. See also: Hexagonal or Clean Architecture. Sticking serde attributes to every type is not recommended.
Use a simple newtype for each use case and tune the serde attributes according to your needs. Like you did in your example. Tedious but worth it.
Valid exception: Date/time libraries that use standardized string formats (ISO/RFC) for serialization and offer support for serde as an optional feature.
I've created a pull request implementing basic serde support for *Type enums.
If it doesn't get merged, it is not a big issue. I can continue creating duplicate types like I've been doing.
@uklotzde brings some valid points about separation of concerns and about formatting (though I do believe serde just parses from the name and does not transform the string by default).
Activity
uklotzde commentedon Aug 2, 2025
I don't think that this would be suitable. As long as there is no canonical, serialized representation for those types it should not be implemented in lofty.
Lepidopteran commentedon Aug 2, 2025
I don't believe I fully understand why this wouldn't be suitable.
Wouldn't adding serde as an optional dependency like this:
Then derive serde::Serialize & serde::DeSerialize to the types that can automatically be serialized by serde if the feature is enabled be sufficient?
Example adding optional serde support for
TagType:If I'm misunderstanding something, please let me know.
Again if this feature can/should not be added, it's not a huge deal, all I have to do is create duplicate types in my project that derives from serde::Serialize and serde::Deserialize like so:
Serial-ATA commentedon Aug 2, 2025
I could see the value in having the basic
*Typeenums (FileType,TagType,PictureType,MimeType) support serialization for use in a database. They'd all just be basic strings anyway. I'd avoid something likeItemKey, though.Feel free to make a PR if you want :)
Lepidopteran commentedon Aug 2, 2025
Will Do! Would you like to me to exempt
PictureInformationand just do the*Typeenums?uklotzde commentedon Aug 2, 2025
Not everything that is technically possible or easy is sensible.
IMHO it is out of scope of this library and would add an unnecessary maintenance burden.
Even for simple strings you would need to make an opinionated decision which formatting to use (
camelCase,PascalCase, ...). This might be inconsistent with how enumerations are supposed to be serialized in the outer/enclosing context. It is unlikely that the lofty types are used in isolation.In non-trivial projects you should always keep the serialization or schema separate from the Domain Model. APIs and persistence have different requirements for serialization. See also: Hexagonal or Clean Architecture. Sticking
serdeattributes to every type is not recommended.Use a simple newtype for each use case and tune the
serdeattributes according to your needs. Like you did in your example. Tedious but worth it.Valid exception: Date/time libraries that use standardized string formats (ISO/RFC) for serialization and offer support for
serdeas an optional feature.Lepidopteran commentedon Aug 2, 2025
I've created a pull request implementing basic serde support for
*Typeenums.If it doesn't get merged, it is not a big issue. I can continue creating duplicate types like I've been doing.
@uklotzde brings some valid points about separation of concerns and about formatting (though I do believe serde just parses from the name and does not transform the string by default).
Serial-ATA commentedon Aug 2, 2025
Casing should only ever matter for keys, not values. At least as far as I can imagine.
Looks like
imagealso offersserdesupport for simple enums:https://docs.rs/image/latest/src/image/image.rs.html#26