Skip to content

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public class EnumSerializer
*/
protected final EnumValues _valuesByEnumNaming;

/**
* Map that contains pre-resolved values for {@link 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;
Copy link
Member

Choose a reason for hiding this comment

The 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 EnumValues members' javadoc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the review - can you elaborate on what you mean here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally thought it will be easier to read if javadoc of _values, _valuesByEnumNaming, and _valuesByToString had similarly structured paragraph and use similar terms. But can skip my comment, thanks 👍🏻


/*
/**********************************************************
/* Construction, initialization
Expand All @@ -71,6 +80,7 @@ public EnumSerializer(EnumValues v, Boolean serializeAsIndex)
_values = v;
_serializeAsIndex = serializeAsIndex;
_valuesByEnumNaming = null;
_valuesByToString = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just delegate (with this(v, serializeAsIndex, null, null)).

Also, unless called from non-deprecated code, let's also mark as @Deprecated // since 2.16.

}

/**
Expand All @@ -82,6 +92,20 @@ public EnumSerializer(EnumValues v, Boolean serializeAsIndex, EnumValues valuesB
_values = v;
_serializeAsIndex = serializeAsIndex;
_valuesByEnumNaming = valuesByEnumNaming;
_valuesByToString = null;
Copy link
Member

@cowtowncoder cowtowncoder Jul 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just use this(v, serializeAsIndex, valuesByEnumNaming, null); ?

Also let's add @Deprecated (and Javadoc) here

}

/**
* @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;
}

/**
Expand All @@ -100,8 +124,9 @@ public static EnumSerializer construct(Class<?> enumClass, SerializationConfig c
*/
EnumValues v = EnumValues.constructFromName(config, beanDesc.getClassInfo());
EnumValues valuesByEnumNaming = constructEnumNamingStrategyValues(config, (Class<Enum<?>>) enumClass, beanDesc.getClassInfo());
EnumValues valuesByToString = EnumValues.constructFromToString(config, beanDesc.getClassInfo());
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(enumClass, format, true, null);
return new EnumSerializer(v, serializeAsIndex, valuesByEnumNaming);
return new EnumSerializer(v, serializeAsIndex, valuesByEnumNaming, valuesByToString);
}

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -205,8 +230,8 @@ 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()) {
enums.add(e.toString());
for (SerializableString value : _valuesByToString.values()) {
enums.add(value.getValue());
}
} else {
// No, serialize using name() or explicit overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private EnumValues(Class<Enum<?>> enumClass, SerializableString[] textual)
*/
public static EnumValues construct(SerializationConfig config, AnnotatedClass annotatedClass) {
if (config.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) {
return constructFromToString(config, _enumClass(annotatedClass.getRawType()));
return constructFromToString(config, annotatedClass);
}
return constructFromName(config, annotatedClass);
}
Expand Down Expand Up @@ -104,6 +104,44 @@ public static EnumValues constructFromName(MapperConfig<?> config, AnnotatedClas
return construct(enumCls, textual);
}

/**
* @since 2.16
*/
public static EnumValues constructFromToString(MapperConfig<?> config, AnnotatedClass annotatedClass)
{
// prepare data
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
final boolean useLowerCase = config.isEnabled(EnumFeature.WRITE_ENUMS_TO_LOWERCASE);
final Class<?> enumCls0 = annotatedClass.getRawType();
final Class<Enum<?>> enumCls = _enumClass(enumCls0);
final Enum<?>[] enumConstants = _enumConstants(enumCls0);

// introspect
String[] names = new String[enumConstants.length];
if (ai != null) {
ai.findEnumValues(config, annotatedClass, enumConstants, names);
}

// build
SerializableString[] textual = new SerializableString[enumConstants.length];
for (int i = 0; i < enumConstants.length; i++) {
String name = names[i];
if (name == null) {
Enum<?> en = enumConstants[i];
name = en.toString();
}
if (useLowerCase) {
name = name.toLowerCase();
}
textual[i] = config.compileString(name);
}
return construct(enumCls, textual);
}

/**
* @deprecated since 2.16; use {@link #constructFromToString(MapperConfig, AnnotatedClass)} instead
*/
@Deprecated
public static EnumValues constructFromToString(MapperConfig<?> config, Class<Enum<?>> enumClass)
{
Class<? extends Enum<?>> cls = ClassUtil.findEnumType(enumClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ public void testEnumsWithJsonProperty() throws Exception {
assertEquals(q("aleph"), MAPPER.writeValueAsString(EnumWithJsonProperty.A));
}

public void testEnumsWithJsonPropertyEnableToString() throws Exception {
String result = MAPPER.writerFor(EnumWithJsonProperty.class)
.with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.writeValueAsString(EnumWithJsonProperty.A);
assertEquals(q("aleph"), result);
}

// [databind#1535]
public void testEnumKeysWithJsonProperty() throws Exception {
Map<EnumWithJsonProperty,Integer> input = new HashMap<EnumWithJsonProperty,Integer>();
Expand Down