-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Feature : Support naming strategy for enums #3792
Feature : Support naming strategy for enums #3792
Conversation
Can I get some test case ideas on EnumNamingStrategies.CamelCaseStrategy? Currently the its implementation is exactly same as |
|
||
public interface EnumNamingStrategy { | ||
|
||
public String translate(String value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add Javadocs to explain direction, and name argument as something more specific than value
.
I think that value
would be Enum.name()
to translate, and result is the "external" value used in JSON, right?
synchronized (this) { | ||
namingStrategy = _namingStrategy; | ||
if (namingStrategy == null) { | ||
EnumNaming enumNamingAnnotation = _valueClass.getAnnotation(EnumNaming.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No; all access to annotations should go via AnnotationIntrospector
, not check directly.
Ok looks like a good starting point. Some suggestions:
|
Thanks you for the review! 🙏🏼 |
src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
Outdated
Show resolved
Hide resolved
src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSerializer.java
Outdated
Show resolved
Hide resolved
* | ||
* @since 2.15 | ||
*/ | ||
public static class CamelCaseStrategy implements EnumNamingStrategy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation of convertEnumToExternalName
here is from Guava's com.google.base.CaseFormat utility class.
e1f405f
to
3556f6e
Compare
3556f6e
to
c67804f
Compare
@@ -253,6 +272,8 @@ protected Object _fromString(JsonParser p, DeserializationContext ctxt, | |||
{ | |||
CompactStringObjectMap lookup = ctxt.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING) | |||
? _getToStringLookup(ctxt) : _lookupByName; | |||
lookup = _hasEnumNaming(ctxt) ? _getEnumNamingLookup(ctxt) : lookup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, getting better, but I think all of introspection really should be done when constructing EnumDeserializer
, and not dynamically. That avoids having to use synchronization and we just replace names used when constructing/initializing deserializer.
synchronized (this) { | ||
lookup = _lookupByEnumNaming; | ||
if (lookup == null) { | ||
EnumNamingStrategy namingStrategy = EnumPropertiesCollector |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be fetched via AnnotationIntrospector
, directly. Or, if we prefer, via DeserializationContext
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Running introspection during construction like you suggested will doors to more information, so the helper class EnumPropertiesCollector
will not be needed.
src/main/java/com/fasterxml/jackson/databind/introspect/EnumPropertiesCollector.java
Outdated
Show resolved
Hide resolved
Ok good, I can see this forming: functionality is there just need to (I think) rearrange things slightly -- mostly to handle all renaming, I think, when constructing I added some notes but probably not enough. I'll try to give some more feedback when I have time. |
// 19-Oct-2016, tatu: Used to just return DEFAULT_KEY_SERIALIZER but why not: | ||
return new Default(Default.TYPE_TO_STRING, rawKeyType); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right above this method, there is exact same method without EnumSerializer.constructEnumNamingStrategyValues(config, (Class<Enum<?>>) rawKeyType, annotatedClass))
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think method above should be marked as @deprecated unless it is actually called from somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Added @deprecated annotation and documented it
Suggested changes based on the feedback provided by @cowtowncoder seem have been implemented. Changes made include:
|
src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java
Show resolved
Hide resolved
src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
Outdated
Show resolved
Hide resolved
Ok this looks pretty good. I wish there was a way to combine 2 instances of I think I'll want to go over the code one more time but maybe get it merged tomorrow. Thank you very much @JooHyukKim for all the improvements here... this should be a nice new feature once we get it merged in for 2.15.0. |
Thank you for for yall's reviews also ✌️ |
src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java
Outdated
Show resolved
Hide resolved
* | ||
* @since 2.15 | ||
*/ | ||
public class EnumPropertiesCollector { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename as... maybe EnumNamingStrategyFactory
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I was expecting this class would be come Enum version of Pojo's POJOPropertiesCollector
. But I guess time will tell. I changed the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had expected that this class would become the Enum version of POJOPropertiesCollector
for Pojos, but it is not yet clear and you are right. I renamed the class accordingly. 🙏🏼
Whoa. This was gnarly to merge into main/3.0. :) |
Niceee |
Description
@EnumNaming
,EnumNamingStrategy
to allow use of naming strategies for Enums #2667