Skip to content

Commit cd43469

Browse files
committed
Merge pull request msgpack#205 from kutchar/add-bigdecimal-support
Added support for BigDecimal, fixes msgpack#204
2 parents ae03bb8 + c10dece commit cd43469

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.core.ObjectCodec;
66
import com.fasterxml.jackson.core.base.GeneratorBase;
77
import com.fasterxml.jackson.core.json.JsonWriteContext;
8+
89
import org.msgpack.core.MessagePacker;
910
import org.msgpack.core.buffer.OutputStreamBufferOutput;
1011

@@ -17,6 +18,7 @@
1718
import java.util.ArrayList;
1819
import java.util.LinkedList;
1920
import java.util.List;
21+
import java.math.MathContext;
2022

2123
public class MessagePackGenerator extends GeneratorBase {
2224
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
@@ -169,8 +171,20 @@ else if (v instanceof BigInteger) {
169171
messagePacker.packBigInteger((BigInteger) v);
170172
}
171173
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+
}
174188
}
175189
else if (v instanceof Boolean) {
176190
messagePacker.packBoolean((Boolean) v);

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,24 @@ public double getDoubleValue() throws IOException, JsonParseException {
269269

270270
@Override
271271
public BigDecimal getDecimalValue() throws IOException {
272-
return null;
272+
ValueRef ref = valueHolder.getRef();
273+
274+
if (ref.isInteger()) {
275+
NumberValue number = ref.asNumber();
276+
//optimization to not convert the value to BigInteger unnecessarily
277+
if (number.isValidByte() || number.isValidShort() || number.isValidInt() || number.isValidLong()) {
278+
return BigDecimal.valueOf(number.asInt());
279+
}
280+
else {
281+
return new BigDecimal(number.asBigInteger());
282+
}
283+
}
284+
285+
if (ref.isFloat()) {
286+
return BigDecimal.valueOf(ref.asFloat().toDouble());
287+
}
288+
289+
throw new UnsupportedOperationException("couldn't parse value as BigDecimal");
273290
}
274291

275292
@Override

0 commit comments

Comments
 (0)