Skip to content

Commit 6eb32dc

Browse files
committed
minor optimization for case-insensitive enum lookup
1 parent d455965 commit 6eb32dc

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
211211
if ((_caseInsensitive == Boolean.TRUE) ||
212212
((_caseInsensitive == null) &&
213213
ctxt.isEnabled(DeserializationFeature.READ_ENUMS_IGNORING_CASE))) {
214-
for (String key : lookup.keys()) {
215-
if (key.equalsIgnoreCase(name)) {
216-
return lookup.find(key);
217-
}
214+
Object match = lookup.findCaseInsensitive(name);
215+
if (match != null) {
216+
return match;
218217
}
219218
} else if (!ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
220219
// [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)

src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ private final Object _find2(String key, int slot, Object match)
124124
return null;
125125
}
126126

127+
/**
128+
* @since 2.9
129+
*/
130+
public Object findCaseInsensitive(String key) {
131+
for (int i = 0, end = _hashArea.length; i < end; i += 2) {
132+
Object k2 = _hashArea[i];
133+
if (k2 != null) {
134+
String s = (String) k2;
135+
if (s.equalsIgnoreCase(key)) {
136+
return _hashArea[i+1];
137+
}
138+
}
139+
}
140+
return null;
141+
}
142+
127143
public List<String> keys() {
128144
final int end = _hashArea.length;
129145
List<String> keys = new ArrayList<String>(end >> 2);

src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumAltIdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException {
8787

8888
public void testIgnoreCaseInEnumList() throws Exception {
8989
TestEnum[] enums = READER_IGNORE_CASE.forType(TestEnum[].class)
90-
.readValue("[\"jackson\", \"rules\"]");
90+
.readValue("[\"jacksON\", \"ruLes\"]");
9191

9292
assertEquals(2, enums.length);
9393
assertEquals(TestEnum.JACKSON, enums[0]);

0 commit comments

Comments
 (0)