Skip to content

Commit d9ac438

Browse files
authored
test case for serializing custom numbers with config overrides (#5199)
1 parent f0c4f70 commit d9ac438

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/test/java/com/fasterxml/jackson/databind/ser/jdk/NumberSerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider se
117117
}
118118
}
119119

120+
static class MyBigDecimal extends BigDecimal {
121+
public MyBigDecimal(String value) {
122+
super(value);
123+
}
124+
}
125+
120126
/*
121127
/**********************************************************
122128
/* Test methods
@@ -222,6 +228,24 @@ public void testCustomSerializationBigDecimalAsNumber() throws Exception {
222228
assertEquals(a2q("{'value':2.0}"), mapper.writeValueAsString(new BigDecimalHolder("2")));
223229
}
224230

231+
@Test
232+
public void testConfigOverrideJdkNumber() throws Exception {
233+
ObjectMapper mapper = jsonMapperBuilder().withConfigOverride(BigDecimal.class,
234+
c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
235+
.build();
236+
String value = mapper.writeValueAsString(new BigDecimal("123.456"));
237+
assertEquals(a2q("'123.456'"), value);
238+
}
239+
240+
@Test
241+
public void testConfigOverrideNonJdkNumber() throws Exception {
242+
ObjectMapper mapper = jsonMapperBuilder().withConfigOverride(MyBigDecimal.class,
243+
c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
244+
.build();
245+
String value = mapper.writeValueAsString(new MyBigDecimal("123.456"));
246+
assertEquals(a2q("'123.456'"), value);
247+
}
248+
225249
// default locale is en_US
226250
static DecimalFormat createDecimalFormatForDefaultLocale(final String pattern) {
227251
return new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.ENGLISH));

0 commit comments

Comments
 (0)