Skip to content

Commit 15d4fe2

Browse files
authored
Merge pull request #1520 from AnaEliza/master
Issue #1313: New IgnoreCase feature to deserialize enums
2 parents cb38b3d + 10b6d51 commit 15d4fe2

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public enum DeserializationFeature implements ConfigFeature
106106
*/
107107
READ_ENUMS_USING_TO_STRING(false),
108108

109+
/**
110+
* Feature that determines if Enum deserialization should be case sensitive or not.
111+
* If enabled, Enum deserialization will ignore case.
112+
* <p>
113+
* Feature is disabled by default.
114+
*/
115+
READ_ENUMS_IGNORING_CASE(false),
116+
109117
/*
110118
/******************************************************
111119
* Error handling features

src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
167167
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
168168
return null;
169169
}
170+
} else if (ctxt.isEnabled(DeserializationFeature.READ_ENUMS_IGNORING_CASE)) {
171+
// [databind#1313]: Case insensitive enum deserialization
172+
for (String key : lookup.keys()) {
173+
if (key.equalsIgnoreCase(name)) {
174+
return lookup.find(key);
175+
}
176+
}
170177
} else if (!ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
171178
// [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)
172179
char c = name.charAt(0);

src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java

+53
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1212
import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer;
1313
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
14+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
1415
import com.fasterxml.jackson.databind.module.SimpleModule;
1516

1617
@SuppressWarnings("serial")
@@ -510,4 +511,56 @@ public void testExceptionFromCustomEnumKeyDeserializer() {
510511
assertTrue(e.getMessage().contains("Undefined AnEnum"));
511512
}
512513
}
514+
515+
public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException {
516+
ObjectMapper objectMapper = new ObjectMapper();
517+
518+
try {
519+
objectMapper.readValue("\"Jackson\"", TestEnum.class);
520+
fail("InvalidFormatException expected");
521+
} catch (InvalidFormatException e) {
522+
assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [JACKSON, OK, RULES]"));
523+
}
524+
}
525+
526+
public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException {
527+
ObjectMapper objectMapper = new ObjectMapper();
528+
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
529+
530+
try {
531+
objectMapper.readValue("\"A\"", LowerCaseEnum.class);
532+
fail("InvalidFormatException expected");
533+
} catch (InvalidFormatException e) {
534+
assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [a, b, c]"));
535+
}
536+
}
537+
538+
public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException {
539+
ObjectMapper objectMapper = new ObjectMapper();
540+
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);
541+
542+
TestEnum testEnum = objectMapper.readValue("\"jackson\"", TestEnum.class);
543+
544+
assertEquals(TestEnum.JACKSON, testEnum);
545+
}
546+
547+
public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException {
548+
ObjectMapper objectMapper = new ObjectMapper();
549+
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
550+
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);
551+
552+
LowerCaseEnum lowerCaseEnum = objectMapper.readValue("\"A\"", LowerCaseEnum.class);
553+
554+
assertEquals(LowerCaseEnum.A, lowerCaseEnum);
555+
}
556+
557+
public void testIgnoreCaseInEnumList() throws Exception {
558+
ObjectMapper objectMapper = new ObjectMapper();
559+
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);
560+
561+
TestEnum[] testEnum = objectMapper.readValue("[\"jackson\", \"rules\"]", TestEnum[].class);
562+
563+
assertEquals(TestEnum.JACKSON, testEnum[0]);
564+
assertEquals(TestEnum.RULES, testEnum[1]);
565+
}
513566
}

0 commit comments

Comments
 (0)