-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Use @JsonProperty
and lowercase feature when serializing Enums despite write using toString()
#4039
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
Changes from 1 commit
4582a5d
450b7e2
52c6a24
6049740
c41185b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,15 @@ public class EnumSerializer | |
*/ | ||
protected final EnumValues _valuesByEnumNaming; | ||
|
||
/** | ||
* Map that contains pre-resolved values for Enum#toString to use for serialization, | ||
* while respecting {@link com.fasterxml.jackson.annotation.JsonProperty} | ||
* and {@link com.fasterxml.jackson.databind.cfg.EnumFeature#WRITE_ENUMS_TO_LOWERCASE}. | ||
* | ||
* @since 2.16 | ||
*/ | ||
protected final EnumValues _valuesByToString; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not confident about readability perspective, but is it possible to use the same/similar structure as other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate the review - can you elaborate on what you mean here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally thought it will be easier to read if javadoc of |
||
|
||
/* | ||
/********************************************************** | ||
/* Construction, initialization | ||
|
@@ -71,6 +80,7 @@ public EnumSerializer(EnumValues v, Boolean serializeAsIndex) | |
_values = v; | ||
_serializeAsIndex = serializeAsIndex; | ||
_valuesByEnumNaming = null; | ||
_valuesByToString = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just delegate (with Also, unless called from non-deprecated code, let's also mark as |
||
} | ||
|
||
/** | ||
|
@@ -82,6 +92,20 @@ public EnumSerializer(EnumValues v, Boolean serializeAsIndex, EnumValues valuesB | |
_values = v; | ||
_serializeAsIndex = serializeAsIndex; | ||
_valuesByEnumNaming = valuesByEnumNaming; | ||
_valuesByToString = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should just use Also let's add |
||
} | ||
|
||
/** | ||
* @since 2.16 | ||
*/ | ||
public EnumSerializer(EnumValues v, Boolean serializeAsIndex, EnumValues valuesByEnumNaming, | ||
EnumValues valuesByToString) | ||
{ | ||
super(v.getEnumClass(), false); | ||
_values = v; | ||
_serializeAsIndex = serializeAsIndex; | ||
_valuesByEnumNaming = valuesByEnumNaming; | ||
_valuesByToString = valuesByToString; | ||
} | ||
|
||
/** | ||
|
@@ -99,9 +123,10 @@ public static EnumSerializer construct(Class<?> enumClass, SerializationConfig c | |
* handle toString() case dynamically (for example) | ||
*/ | ||
EnumValues v = EnumValues.constructFromName(config, beanDesc.getClassInfo()); | ||
EnumValues valuesByToString = EnumValues.constructFromToString(config, beanDesc.getClassInfo()); | ||
iProdigy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
EnumValues valuesByEnumNaming = constructEnumNamingStrategyValues(config, (Class<Enum<?>>) enumClass, beanDesc.getClassInfo()); | ||
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(enumClass, format, true, null); | ||
return new EnumSerializer(v, serializeAsIndex, valuesByEnumNaming); | ||
return new EnumSerializer(v, serializeAsIndex, valuesByEnumNaming, valuesByToString); | ||
} | ||
|
||
/** | ||
|
@@ -154,7 +179,7 @@ public final void serialize(Enum<?> en, JsonGenerator gen, SerializerProvider se | |
} | ||
// [databind#749]: or via toString()? | ||
if (serializers.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) { | ||
gen.writeString(en.toString()); | ||
gen.writeString(_valuesByToString.serializedValueFor(en)); | ||
return; | ||
} | ||
gen.writeString(_values.serializedValueFor(en)); | ||
|
@@ -205,7 +230,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t | |
// Use toString()? | ||
if ((serializers != null) && | ||
serializers.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) { | ||
for (Enum<?> e : _values.enums()) { | ||
for (Enum<?> e : _valuesByToString.enums()) { | ||
enums.add(e.toString()); | ||
} | ||
iProdigy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
|
Uh oh!
There was an error while loading. Please reload this page.