-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow serialization/deserialization of enums using kebab-case
.
#5092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've added Stack Overflow question Change Jackson deserialization of all enums without using annotations to help find a temporary workaround. |
I wasn't aware there were these existing naming strategies. Is there a way to choose one of them in my object mapper? Or are you saying that is not way at the moment? |
Those Naming strategies are real. You may find them in test cases. And... mapper.customEnumNamingStrategy(
YourCustomEnum.class
/* source= */ UPPER_SNAKE_CASE,
/* target= */ KEBAB_CASE
) ... this is non-existent feature. Sorry if I confused u :-0 |
Thanks for that information, @JooHyukKim . Do you know of a quick-and-dirty way I can add a custom enum deserializer at the the object mapper level for all enum types as a temporary workaround? |
Enum deserializers can be registered like any other, by registering
and registration via |
On
but while Also: |
Well my framework is still in development, so if I can use v2.19.0-rc2 right now and call |
Excellent! I hope it works -- being a newer feature there can be rough edges but conceptually it should work. Plus a good way for us to get feedback |
For future reference it seems the naming strategies were added in issue #2667, but I didn't see where the |
@garretwilson Hmmh. I think #4676 would be the issue, PR #4728. |
I don't see any deserialization tests of |
I haven't tried v2.19.0-rc2 yet—I'm exploring some other avenues. But in my exploration and from looking at the code, there's a potential problem you might want to look at. Let me ask you a question: with default enum deserialization, if Jackson is deserializing the enum value But what if I turn on But I don't even see any deserialization code for |
I did not implement this feature so I don't know off-hand, but one thing to note on all Naming Strategies -- they work statically, mapping class defined names, without considering possible input. So it's all about going from implied name (from Enum constant) to expected/produced output name; and there can only be a single mapping for each Enum constant. From that, incoming String is never changed to anything; but it is expected to match "external name" produced from Enum name. @lbenedetto Might be better able to answer here, having provided the PR. |
Oh, I see—so they don't do a reverse transformation on the serialized value, but rather transform the expected enum values and compare those to what the input is? OK, then that would keep case sensitivity, and would also explain why I didn't see any reverse transformation code. Thanks for the explanation. |
Yes, exactly. |
@garretwilson Can this be closed? |
kebab-case
.kebab-case
.
@cowtowncoder thanks for checking back. Yes, I think the Note however that in the end I opted to add serialization/deserialization manually using serialization and deserialization modifiers, which I discovered from an answer to my question Change Jackson deserialization of all enums without using annotations on Stack Overflow. The reason is that I'm creating a framework and I'd like all the enums to use So in a future improvement to my framework, I'm going to add checks for Jackson annotations on each serialized type to the serializer/deserializer modifiers, and if any such annotations are present I'll just delegate to the default serializers/deserializers. (This is the major benefit for me of the modifiers—they provide access to the default serializer/deserializer.) If you're interested you can read more details on my ticket FLANGE-30. As always thanks for your attentiveness and communication. |
@garretwilson Makes total sense & thank you for sharing your situation, context and solutions. Modifiers are indeed powerful and flexible ways to augment (or replace) existing handling; glad you found them and managed to get things working the way you want! |
Is your feature request related to a problem? Please describe.
The Java world (and many other languages) uses mostly
camelCase
identifiers, except for constants (in particular enums), which are identified withUPPER_SNAKE_CASE
. However the modern web world uses (lowercase)kebab-case
for identifiers. You can see this in:Modern web frameworks such as Angular and Vue translate between the two worlds (in this case, the worlds of JavaScript/TypeScript and the web) transparently. Similarly I'm sure you're aware that JavaScript-based DOM methods use
camelCase
identifiers when accessingkebab-case
data, both for standard attributes and actual custom data attributes.I'm creating an entire framework that needs to convert Java enum
UPPER_SNAKE_CASE
tokebab-case
and back transparently when serializing/deserializing.Issue #3053 got us half the way there, but really that feature request was specific to the requestor's use case. I'm not saying that functionality is never useful. I'm saying that the usage of
kebab-case
(this request) in the modern web world is overwhelmingly more needed thanlower_snake_case
(which is effectively what #3053 produces).Describe the solution you'd like
We need a feature to tell Jackson to serialize/deserialize an enum value such as
FOO_BAR
asfoo-bar
. PerhapsEnumFeature.WRITE_ENUMS_TO_KEBAB_CASE
would work. (I assume/hope that the implementation of #3053 handles deserialization as well? I haven't tried it.)Usage example
No response
Additional context
As I workaround, I've thrown together part of a proof of concept to override serialization. I could use some help on this so I can complete it and use it in the interim:
This works for serialization, although it's a shame to use raw
Enum
. I couldn't immediately figure out a way to prevent the warning, even usingEnum<?>
and the like.However I don't see how to create a deserializer that will override the behavior for all enums. The
com.fasterxml.jackson.databind.deser.std.EnumDeserializer
class is huge! Is there some way to override that and do a pre-transformation on the token or something? Or is there a simpler way to add a new custom enum serializer with this functionality?How best to add a quick-and-dirty workaround until (and if) Jackson adds this feature natively? Thanks.
The text was updated successfully, but these errors were encountered: