Skip to content

Commit 1847015

Browse files
committed
Implemented #149
1 parent 0a4251a commit 1847015

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Version: 2.4.0 (xx-xxx-2014)
33

44
#88: Prevent use of type information for `JsonNode` via default typing
55
(reported by electricmonk@github)
6+
#149: Allow use of "stringified" indexes for Enum values
7+
(requested by chenboxiang@github)
68
#335: Allow use of `@JsonProperytOrder(alphabetic=true)` for Map properties
79
#353: Problems with polymorphic types, `JsonNode` (related to #88)
810
(reported by cemo@github)

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,25 @@ public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt)
8282
String name = jp.getText();
8383
Enum<?> result = _resolver.findEnum(name);
8484
if (result == null) {
85-
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
86-
if (name.length() == 0 || name.trim().length() == 0) {
85+
name = name.trim();
86+
if (name.length() == 0) {
87+
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
8788
return null;
8889
}
90+
} else {
91+
// [#149]: Allow use of 'String' indexes as well
92+
char c = name.charAt(0);
93+
if (c >= '0' && c <= '9') {
94+
try {
95+
int ix = Integer.parseInt(name);
96+
result = _resolver.getEnum(ix);
97+
if (result != null) {
98+
return result;
99+
}
100+
} catch (NumberFormatException e) {
101+
// fine, ignore, was not an integer
102+
}
103+
}
89104
}
90105
if (!ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
91106
throw ctxt.weirdStringException(name, _resolver.getEnumClass(),

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
public class TestEnumDeserialization
2020
extends BaseMapTest
2121
{
22-
/*
23-
/**********************************************************
24-
/* Helper classes, enums
25-
/**********************************************************
26-
*/
27-
2822
enum TestEnum { JACKSON, RULES, OK; }
2923

3024
/**
@@ -394,4 +388,16 @@ public void testUnwrappedEnumException() throws Exception {
394388
//exception as thrown correctly
395389
}
396390
}
391+
392+
// [Issue#149]: 'stringified' indexes for enums
393+
public void testIndexAsString() throws Exception
394+
{
395+
// first, regular index ought to work fine
396+
TestEnum en = MAPPER.readValue("2", TestEnum.class);
397+
assertSame(TestEnum.values()[2], en);
398+
399+
// but also with quoted Strings
400+
en = MAPPER.readValue(quote("1"), TestEnum.class);
401+
assertSame(TestEnum.values()[1], en);
402+
}
397403
}

0 commit comments

Comments
 (0)