|
7 | 7 | import com.fasterxml.jackson.annotation.*; |
8 | 8 | import com.fasterxml.jackson.core.*; |
9 | 9 | import com.fasterxml.jackson.core.type.TypeReference; |
| 10 | + |
10 | 11 | import com.fasterxml.jackson.databind.*; |
11 | 12 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
12 | 13 | import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer; |
13 | 14 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
14 | 15 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
15 | 16 | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| 17 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
16 | 18 | import com.fasterxml.jackson.databind.module.SimpleModule; |
17 | 19 |
|
18 | 20 | @SuppressWarnings("serial") |
@@ -183,6 +185,25 @@ public static ObjectMapper setupObjectMapper(ObjectMapper mapper) { |
183 | 185 | } |
184 | 186 | } |
185 | 187 |
|
| 188 | + // for [databind#2309] |
| 189 | + static enum Enum2309 { |
| 190 | + NON_NULL("NON_NULL"), |
| 191 | + NULL(null), |
| 192 | + OTHER("OTHER") |
| 193 | + ; |
| 194 | + |
| 195 | + private String value; |
| 196 | + |
| 197 | + private Enum2309(String value) { |
| 198 | + this.value = value; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public String toString() { |
| 203 | + return value; |
| 204 | + } |
| 205 | + } |
| 206 | + |
186 | 207 | /* |
187 | 208 | /********************************************************** |
188 | 209 | /* Test methods |
@@ -290,11 +311,11 @@ public void testNumbersToEnums() throws Exception |
290 | 311 |
|
291 | 312 | public void testEnumsWithIndex() throws Exception |
292 | 313 | { |
293 | | - ObjectMapper m = new ObjectMapper(); |
294 | | - m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX); |
295 | | - String json = m.writeValueAsString(TestEnum.RULES); |
| 314 | + String json = MAPPER.writer() |
| 315 | + .with(SerializationFeature.WRITE_ENUMS_USING_INDEX) |
| 316 | + .writeValueAsString(TestEnum.RULES); |
296 | 317 | assertEquals(String.valueOf(TestEnum.RULES.ordinal()), json); |
297 | | - TestEnum result = m.readValue(json, TestEnum.class); |
| 318 | + TestEnum result = MAPPER.readValue(json, TestEnum.class); |
298 | 319 | assertSame(TestEnum.RULES, result); |
299 | 320 | } |
300 | 321 |
|
@@ -391,10 +412,10 @@ public void testGenericEnumDeserialization() throws Exception |
391 | 412 |
|
392 | 413 | // [databind#381] |
393 | 414 | public void testUnwrappedEnum() throws Exception { |
394 | | - final ObjectMapper mapper = newJsonMapper(); |
395 | | - mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS); |
396 | | - |
397 | | - assertEquals(TestEnum.JACKSON, mapper.readValue("[" + quote("JACKSON") + "]", TestEnum.class)); |
| 415 | + assertEquals(TestEnum.JACKSON, |
| 416 | + MAPPER.readerFor(TestEnum.class) |
| 417 | + .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) |
| 418 | + .readValue("[" + quote("JACKSON") + "]")); |
398 | 419 | } |
399 | 420 |
|
400 | 421 | public void testUnwrappedEnumException() throws Exception { |
@@ -422,11 +443,12 @@ public void testIndexAsString() throws Exception |
422 | 443 | assertSame(TestEnum.values()[1], en); |
423 | 444 |
|
424 | 445 | // [databind#1690]: unless prevented |
425 | | - final ObjectMapper mapper = jsonMapperBuilder() |
426 | | - .disable(MapperFeature.ALLOW_COERCION_OF_SCALARS) |
427 | | - .build(); |
428 | 446 | try { |
429 | | - en = mapper.readValue(quote("1"), TestEnum.class); |
| 447 | + en = JsonMapper.builder() |
| 448 | + .configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false) |
| 449 | + .build() |
| 450 | + .readerFor(TestEnum.class) |
| 451 | + .readValue(quote("1")); |
430 | 452 | fail("Should not pass"); |
431 | 453 | } catch (MismatchedInputException e) { |
432 | 454 | verifyException(e, "Cannot deserialize value of type"); |
@@ -528,4 +550,13 @@ public void testExceptionFromCustomEnumKeyDeserializer() throws Exception { |
528 | 550 | assertTrue(e.getMessage().contains("Undefined AnEnum")); |
529 | 551 | } |
530 | 552 | } |
| 553 | + |
| 554 | + // [databind#2309] |
| 555 | + public void testEnumToStringNull2309() throws Exception |
| 556 | + { |
| 557 | + Enum2309 value = MAPPER.readerFor(Enum2309.class) |
| 558 | + .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING) |
| 559 | + .readValue(quote("NON_NULL")); |
| 560 | + assertEquals(Enum2309.NON_NULL, value); |
| 561 | + } |
531 | 562 | } |
0 commit comments