Skip to content

Commit ea64970

Browse files
committed
test case for serializing custom numbers with config overrides
1 parent 94e4019 commit ea64970

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/test/java/tools/jackson/databind/ser/jdk/NumberSerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tools.jackson.databind.SerializationContext;
1818
import tools.jackson.databind.ValueSerializer;
1919
import tools.jackson.databind.annotation.JsonSerialize;
20+
import tools.jackson.databind.json.JsonMapper;
2021
import tools.jackson.databind.module.SimpleModule;
2122
import tools.jackson.databind.testutil.DatabindTestUtil;
2223

@@ -117,6 +118,12 @@ public void serialize(BigDecimal value, JsonGenerator gen, SerializationContext
117118
}
118119
}
119120

121+
static class MyBigDecimal extends BigDecimal {
122+
public MyBigDecimal(String value) {
123+
super(value);
124+
}
125+
}
126+
120127
/*
121128
/**********************************************************
122129
/* Test methods
@@ -225,6 +232,15 @@ public void testCustomSerializationBigDecimalAsNumber() throws Exception {
225232
assertEquals(a2q("{'value':2.0}"), mapper.writeValueAsString(new BigDecimalHolder("2")));
226233
}
227234

235+
@Test
236+
public void testConfigOverrideNonJdkNumber() throws Exception {
237+
JsonMapper mapper = jsonMapperBuilder().withConfigOverride(MyBigDecimal.class,
238+
c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
239+
.build();
240+
String value = mapper.writeValueAsString(new MyBigDecimal("123.456"));
241+
assertEquals(a2q("'123.456'"), value);
242+
}
243+
228244
// default locale is en_US
229245
static DecimalFormat createDecimalFormatForDefaultLocale(final String pattern) {
230246
return new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.ENGLISH));

0 commit comments

Comments
 (0)