|
1 | 1 | package tools.jackson.databind.ser.jdk; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.math.BigDecimal; |
4 | 5 | import java.math.BigInteger; |
| 6 | +import java.text.DecimalFormat; |
5 | 7 |
|
6 | 8 | import org.junit.jupiter.api.Test; |
7 | 9 |
|
8 | 10 | import com.fasterxml.jackson.annotation.JsonFormat; |
9 | 11 | import com.fasterxml.jackson.annotation.JsonInclude; |
10 | 12 | import com.fasterxml.jackson.annotation.JsonProperty; |
11 | 13 |
|
| 14 | +import tools.jackson.core.JsonGenerator; |
| 15 | +import tools.jackson.databind.module.SimpleModule; |
12 | 16 | import tools.jackson.databind.ObjectMapper; |
| 17 | +import tools.jackson.databind.SerializerProvider; |
| 18 | +import tools.jackson.databind.ValueSerializer; |
13 | 19 | import tools.jackson.databind.annotation.JsonSerialize; |
14 | 20 | import tools.jackson.databind.testutil.DatabindTestUtil; |
15 | 21 |
|
@@ -80,6 +86,36 @@ static class NumberWrapper { |
80 | 86 | public NumberWrapper(Number v) { value = v; } |
81 | 87 | } |
82 | 88 |
|
| 89 | + static class BigDecimalHolder { |
| 90 | + private final BigDecimal value; |
| 91 | + |
| 92 | + public BigDecimalHolder(String num) { |
| 93 | + value = new BigDecimal(num); |
| 94 | + } |
| 95 | + |
| 96 | + public BigDecimal getValue() { |
| 97 | + return value; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static class BigDecimalAsStringSerializer extends ValueSerializer<BigDecimal> { |
| 102 | + private final DecimalFormat df = new DecimalFormat("0.0"); |
| 103 | + |
| 104 | + @Override |
| 105 | + public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) { |
| 106 | + gen.writeString(df.format(value)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + static class BigDecimalAsNumberSerializer extends ValueSerializer<BigDecimal> { |
| 111 | + private final DecimalFormat df = new DecimalFormat("0.0"); |
| 112 | + |
| 113 | + @Override |
| 114 | + public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) { |
| 115 | + gen.writeNumber(df.format(value)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
83 | 119 | /* |
84 | 120 | /********************************************************** |
85 | 121 | /* Test methods |
@@ -171,4 +207,20 @@ public void testNumberType() throws Exception |
171 | 207 | assertEquals(a2q("{'value':123}"), MAPPER.writeValueAsString(new NumberWrapper(BigInteger.valueOf(123)))); |
172 | 208 | assertEquals(a2q("{'value':0.025}"), MAPPER.writeValueAsString(new NumberWrapper(BigDecimal.valueOf(0.025)))); |
173 | 209 | } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void testCustomSerializationBigDecimalAsString() throws Exception { |
| 213 | + SimpleModule module = new SimpleModule(); |
| 214 | + module.addSerializer(BigDecimal.class, new BigDecimalAsStringSerializer()); |
| 215 | + ObjectMapper mapper = jsonMapperBuilder().addModule(module).build(); |
| 216 | + assertEquals(a2q("{'value':'2.0'}"), mapper.writeValueAsString(new BigDecimalHolder("2"))); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + public void testCustomSerializationBigDecimalAsNumber() throws Exception { |
| 221 | + SimpleModule module = new SimpleModule(); |
| 222 | + module.addSerializer(BigDecimal.class, new BigDecimalAsNumberSerializer()); |
| 223 | + ObjectMapper mapper = jsonMapperBuilder().addModule(module).build(); |
| 224 | + assertEquals(a2q("{'value':2.0}"), mapper.writeValueAsString(new BigDecimalHolder("2"))); |
| 225 | + } |
174 | 226 | } |
0 commit comments