|
3 | 3 | import java.io.Serializable;
|
4 | 4 | import java.util.*;
|
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JsonFormat; |
6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 8 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 9 | +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
7 | 10 |
|
8 | 11 | /**
|
9 | 12 | * Tests stemming from [#12], where `Calendar` fails; however, bit more general
|
10 | 13 | * problem.
|
11 | 14 | */
|
12 | 15 | public class TestJDKTypes extends BaseTest
|
13 | 16 | {
|
| 17 | + static class Bean117UsingJsonFormat { |
| 18 | + @JsonFormat(shape = JsonFormat.Shape.STRING) |
| 19 | + public int value = 42; |
| 20 | + } |
| 21 | + |
| 22 | + static class Bean117UsingJsonSerialize { |
| 23 | + @JsonSerialize(using = ToStringSerializer.class) |
| 24 | + public int value = 42; |
| 25 | + } |
| 26 | + |
14 | 27 | private final ObjectMapper MAPPER = newMrBeanMapper();
|
| 28 | + private final ObjectMapper VANILLA_MAPPER = newPlainJsonMapper(); |
15 | 29 |
|
16 | 30 | public void testDateTimeTypes() throws Exception
|
17 | 31 | {
|
@@ -62,4 +76,18 @@ public void testSerializable() throws Exception
|
62 | 76 | Serializable value = new ObjectMapper().readValue(quote("abc"), Serializable.class);
|
63 | 77 | assertEquals("abc", (String) value);
|
64 | 78 | }
|
| 79 | + |
| 80 | + // [modules-base#117]: should work with "Numbers-as-String" case too |
| 81 | + public void testIntAsString() throws Exception |
| 82 | + { |
| 83 | + final String EXP_JSON = "{\"value\":\"42\"}"; |
| 84 | + |
| 85 | + // First, check usage via `@JsonFormat` |
| 86 | + assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(new Bean117UsingJsonFormat())); |
| 87 | + assertEquals(EXP_JSON, MAPPER.writeValueAsString(new Bean117UsingJsonFormat())); |
| 88 | + |
| 89 | + // then with `@JsonSerialize` |
| 90 | + assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(new Bean117UsingJsonSerialize())); |
| 91 | + assertEquals(EXP_JSON, MAPPER.writeValueAsString(new Bean117UsingJsonSerialize())); |
| 92 | + } |
65 | 93 | }
|
0 commit comments