Skip to content

Commit

Permalink
Merge pull request #1520 from AnaEliza/master
Browse files Browse the repository at this point in the history
Issue #1313: New IgnoreCase feature to deserialize enums
  • Loading branch information
cowtowncoder committed Feb 23, 2017
2 parents cb38b3d + 10b6d51 commit 15d4fe2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Expand Up @@ -106,6 +106,14 @@ public enum DeserializationFeature implements ConfigFeature
*/
READ_ENUMS_USING_TO_STRING(false),

/**
* Feature that determines if Enum deserialization should be case sensitive or not.
* If enabled, Enum deserialization will ignore case.
* <p>
* Feature is disabled by default.
*/
READ_ENUMS_IGNORING_CASE(false),

/*
/******************************************************
* Error handling features
Expand Down
Expand Up @@ -167,6 +167,13 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
return null;
}
} else if (ctxt.isEnabled(DeserializationFeature.READ_ENUMS_IGNORING_CASE)) {
// [databind#1313]: Case insensitive enum deserialization
for (String key : lookup.keys()) {
if (key.equalsIgnoreCase(name)) {
return lookup.find(key);
}
}
} else if (!ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
// [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)
char c = name.charAt(0);
Expand Down
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.module.SimpleModule;

@SuppressWarnings("serial")
Expand Down Expand Up @@ -510,4 +511,56 @@ public void testExceptionFromCustomEnumKeyDeserializer() {
assertTrue(e.getMessage().contains("Undefined AnEnum"));
}
}

public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

try {
objectMapper.readValue("\"Jackson\"", TestEnum.class);
fail("InvalidFormatException expected");
} catch (InvalidFormatException e) {
assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [JACKSON, OK, RULES]"));
}
}

public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);

try {
objectMapper.readValue("\"A\"", LowerCaseEnum.class);
fail("InvalidFormatException expected");
} catch (InvalidFormatException e) {
assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [a, b, c]"));
}
}

public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);

TestEnum testEnum = objectMapper.readValue("\"jackson\"", TestEnum.class);

assertEquals(TestEnum.JACKSON, testEnum);
}

public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);

LowerCaseEnum lowerCaseEnum = objectMapper.readValue("\"A\"", LowerCaseEnum.class);

assertEquals(LowerCaseEnum.A, lowerCaseEnum);
}

public void testIgnoreCaseInEnumList() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE);

TestEnum[] testEnum = objectMapper.readValue("[\"jackson\", \"rules\"]", TestEnum[].class);

assertEquals(TestEnum.JACKSON, testEnum[0]);
assertEquals(TestEnum.RULES, testEnum[1]);
}
}

0 comments on commit 15d4fe2

Please sign in to comment.