Skip to content

Custom global deserializer OR onDeserialization hook #2436

@kotlinlukas

Description

@kotlinlukas

What is your use-case and why do you need this feature?

I want all strings during any deserialization to be trimmed to null, so calling .trim() on the string and then setting it to null if the string is blank. But the second part is technically optional, so if I can just automatically trim all surrounding whitespace from my strings I would be happy.

Currently this is possible, but very ugly - I could mark every single field/file in my entire library with an annotation for a custom deserializer, or use a typealias like TrimmedString to shorten this, but that is prone to errors and inconsistencies (by forgetting) and also just pretty ugly.

Describe the solution you'd like

Option 1 - Allow me to specify a global default deserializer for a class, like other libraries do - so that I can create a custom StringDeserializer, configure deserialization globally (once!) so that if it's a String, it will use that deserializer, and then I can implement my custom functionality

Option 2 - (second preference), add a hook/callback to deserialization and allow me to provide a custom function ( String -> String, or more generically, T -> T) that I can use to operate on the freshly deserialized entity before the value is stored in its destination

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions