Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.base.GeneratorBase;
import com.fasterxml.jackson.core.json.JsonWriteContext;

import org.msgpack.core.MessagePacker;
import org.msgpack.core.buffer.OutputStreamBufferOutput;

Expand All @@ -17,6 +18,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.math.MathContext;

public class MessagePackGenerator extends GeneratorBase {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
Expand Down Expand Up @@ -169,8 +171,20 @@ else if (v instanceof BigInteger) {
messagePacker.packBigInteger((BigInteger) v);
}
else if (v instanceof BigDecimal) {
// TODO
throw new UnsupportedOperationException("BigDecimal isn't supported yet");
BigDecimal decimal = (BigDecimal) v;
try {
//Check to see if this BigDecimal can be converted to BigInteger
BigInteger integer = decimal.toBigIntegerExact();
messagePacker.packBigInteger(integer);
} catch (ArithmeticException e){
//If not an integer, then try converting to double
double doubleValue = decimal.doubleValue();
//Check to make sure this BigDecimal can be represented as a double
if (!new BigDecimal(doubleValue, MathContext.DECIMAL128).equals(decimal)) {
throw new IllegalArgumentException("Messagepack cannot serialize a BigDecimal that can't be represented as double");
}
messagePacker.packDouble(doubleValue);
}
}
else if (v instanceof Boolean) {
messagePacker.packBoolean((Boolean) v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,24 @@ public double getDoubleValue() throws IOException, JsonParseException {

@Override
public BigDecimal getDecimalValue() throws IOException {
return null;
ValueRef ref = valueHolder.getRef();

if (ref.isInteger()) {
NumberValue number = ref.asNumber();
//optimization to not convert the value to BigInteger unnecessarily
if (number.isValidByte() || number.isValidShort() || number.isValidInt() || number.isValidLong()) {
return BigDecimal.valueOf(number.asInt());
}
else {
return new BigDecimal(number.asBigInteger());
}
}

if (ref.isFloat()) {
return BigDecimal.valueOf(ref.asFloat().toDouble());
}

throw new UnsupportedOperationException("couldn't parse value as BigDecimal");
}

@Override
Expand Down