|
5 | 5 | import com.fasterxml.jackson.core.ObjectCodec; |
6 | 6 | import com.fasterxml.jackson.core.base.GeneratorBase; |
7 | 7 | import com.fasterxml.jackson.core.json.JsonWriteContext; |
| 8 | + |
8 | 9 | import org.msgpack.core.MessagePacker; |
9 | 10 | import org.msgpack.core.buffer.OutputStreamBufferOutput; |
10 | 11 |
|
|
17 | 18 | import java.util.ArrayList; |
18 | 19 | import java.util.LinkedList; |
19 | 20 | import java.util.List; |
| 21 | +import java.math.MathContext; |
20 | 22 |
|
21 | 23 | public class MessagePackGenerator extends GeneratorBase { |
22 | 24 | private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); |
@@ -169,8 +171,20 @@ else if (v instanceof BigInteger) { |
169 | 171 | messagePacker.packBigInteger((BigInteger) v); |
170 | 172 | } |
171 | 173 | else if (v instanceof BigDecimal) { |
172 | | - // TODO |
173 | | - throw new UnsupportedOperationException("BigDecimal isn't supported yet"); |
| 174 | + BigDecimal decimal = (BigDecimal) v; |
| 175 | + try { |
| 176 | + //Check to see if this BigDecimal can be converted to BigInteger |
| 177 | + BigInteger integer = decimal.toBigIntegerExact(); |
| 178 | + messagePacker.packBigInteger(integer); |
| 179 | + } catch (ArithmeticException e){ |
| 180 | + //If not an integer, then try converting to double |
| 181 | + double doubleValue = decimal.doubleValue(); |
| 182 | + //Check to make sure this BigDecimal can be represented as a double |
| 183 | + if (!new BigDecimal(doubleValue, MathContext.DECIMAL128).equals(decimal)) { |
| 184 | + throw new IllegalArgumentException("Messagepack cannot serialize a BigDecimal that can't be represented as double"); |
| 185 | + } |
| 186 | + messagePacker.packDouble(doubleValue); |
| 187 | + } |
174 | 188 | } |
175 | 189 | else if (v instanceof Boolean) { |
176 | 190 | messagePacker.packBoolean((Boolean) v); |
|
0 commit comments