Skip to content

Commit e49b9f2

Browse files
committed
Try to reproduce #117
1 parent 16b10b3 commit e49b9f2

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

mrbean/src/test/java/com/fasterxml/jackson/module/mrbean/BaseTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.core.*;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.json.JsonMapper;
1011

1112
//import static org.junit.Assert.*;
1213

@@ -20,7 +21,13 @@ public abstract class BaseTest
2021
*/
2122

2223
protected ObjectMapper newMrBeanMapper() {
23-
return new ObjectMapper().registerModule(new MrBeanModule());
24+
return JsonMapper.builder()
25+
.addModule(new MrBeanModule())
26+
.build();
27+
}
28+
29+
protected ObjectMapper newPlainJsonMapper() {
30+
return new JsonMapper();
2431
}
2532

2633
/*

mrbean/src/test/java/com/fasterxml/jackson/module/mrbean/TestJDKTypes.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@
33
import java.io.Serializable;
44
import java.util.*;
55

6+
import com.fasterxml.jackson.annotation.JsonFormat;
67
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
710

811
/**
912
* Tests stemming from [#12], where `Calendar` fails; however, bit more general
1013
* problem.
1114
*/
1215
public class TestJDKTypes extends BaseTest
1316
{
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+
1427
private final ObjectMapper MAPPER = newMrBeanMapper();
28+
private final ObjectMapper VANILLA_MAPPER = newPlainJsonMapper();
1529

1630
public void testDateTimeTypes() throws Exception
1731
{
@@ -62,4 +76,18 @@ public void testSerializable() throws Exception
6276
Serializable value = new ObjectMapper().readValue(quote("abc"), Serializable.class);
6377
assertEquals("abc", (String) value);
6478
}
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+
}
6593
}

0 commit comments

Comments
 (0)