|
1 | 1 | package com.fasterxml.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 |
|
| 8 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 9 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 10 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
6 | 12 | import org.junit.jupiter.api.Test;
|
7 | 13 |
|
8 | 14 | import com.fasterxml.jackson.annotation.JsonFormat;
|
@@ -79,6 +85,36 @@ static class NumberWrapper {
|
79 | 85 | public NumberWrapper(Number v) { value = v; }
|
80 | 86 | }
|
81 | 87 |
|
| 88 | + static class BigDecimalHolder { |
| 89 | + private final BigDecimal value; |
| 90 | + |
| 91 | + public BigDecimalHolder(String num) { |
| 92 | + value = new BigDecimal(num); |
| 93 | + } |
| 94 | + |
| 95 | + public BigDecimal getValue() { |
| 96 | + return value; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + static class BigDecimalAsStringSerializer extends JsonSerializer<BigDecimal> { |
| 101 | + private final DecimalFormat df = new DecimalFormat("0.0"); |
| 102 | + |
| 103 | + @Override |
| 104 | + public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| 105 | + gen.writeString(df.format(value)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static class BigDecimalAsNumberSerializer extends JsonSerializer<BigDecimal> { |
| 110 | + private final DecimalFormat df = new DecimalFormat("0.0"); |
| 111 | + |
| 112 | + @Override |
| 113 | + public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| 114 | + gen.writeNumber(df.format(value)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
82 | 118 | /*
|
83 | 119 | /**********************************************************
|
84 | 120 | /* Test methods
|
@@ -167,4 +203,20 @@ public void testNumberType() throws Exception
|
167 | 203 | assertEquals(a2q("{'value':123}"), MAPPER.writeValueAsString(new NumberWrapper(BigInteger.valueOf(123))));
|
168 | 204 | assertEquals(a2q("{'value':0.025}"), MAPPER.writeValueAsString(new NumberWrapper(BigDecimal.valueOf(0.025))));
|
169 | 205 | }
|
| 206 | + |
| 207 | + @Test |
| 208 | + public void testCustomSerializationBigDecimalAsString() throws Exception { |
| 209 | + SimpleModule module = new SimpleModule(); |
| 210 | + module.addSerializer(BigDecimal.class, new BigDecimalAsStringSerializer()); |
| 211 | + ObjectMapper mapper = jsonMapperBuilder().addModule(module).build(); |
| 212 | + assertEquals(a2q("{'value':'2.0'}"), mapper.writeValueAsString(new BigDecimalHolder("2"))); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testCustomSerializationBigDecimalAsNumber() throws Exception { |
| 217 | + SimpleModule module = new SimpleModule(); |
| 218 | + module.addSerializer(BigDecimal.class, new BigDecimalAsNumberSerializer()); |
| 219 | + ObjectMapper mapper = jsonMapperBuilder().addModule(module).build(); |
| 220 | + assertEquals(a2q("{'value':2.0}"), mapper.writeValueAsString(new BigDecimalHolder("2"))); |
| 221 | + } |
170 | 222 | }
|
0 commit comments