Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CustomClassMapper#convertBigDecimal for Deserializer #196

Merged
merged 4 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,7 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -324,6 +325,8 @@ private static <T> T deserializeToPrimitive(
return (T) convertDouble(o, context);
} else if (Long.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz)) {
return (T) convertLong(o, context);
} else if (BigDecimal.class.isAssignableFrom(clazz)) {
return (T) convertBigDecimal(o, context);
} else if (Float.class.isAssignableFrom(clazz) || float.class.isAssignableFrom(clazz)) {
return (T) (Float) convertDouble(o, context).floatValue();
} else {
Expand Down Expand Up @@ -462,6 +465,22 @@ private static Double convertDouble(Object o, DeserializeContext context) {
}
}

private static BigDecimal convertBigDecimal(Object o, DeserializeContext context) {
if (o instanceof Integer) {
return BigDecimal.valueOf(((Integer) o).intValue());
} else if (o instanceof Long) {
return BigDecimal.valueOf(((Long) o).longValue());
} else if (o instanceof Double) {
return BigDecimal.valueOf(((Double) o).doubleValue()).abs();
} else if (o instanceof BigDecimal) {
return (BigDecimal) o;
} else {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
throw deserializeError(
context.errorPath,
"Failed to convert a value of type " + o.getClass().getName() + " to BigDecimal");
}
}

private static Boolean convertBoolean(Object o, DeserializeContext context) {
if (o instanceof Boolean) {
return (Boolean) o;
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.google.common.collect.ImmutableList;
import com.google.firestore.v1.DatabaseRootName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -74,6 +75,14 @@ public double getValue() {
}
}

private static class BigDecimalBean {
private BigDecimal value;

public BigDecimal getValue() {
return value;
}
}

private static class FloatBean {
private float value;

Expand Down Expand Up @@ -1049,6 +1058,38 @@ public void primitiveDeserializeDouble() {
}
}

@Test
public void primitiveDeserializeBigDecimal() {
BigDecimalBean beanBigdecimal = deserialize("{'value': 123}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);

// Int
BigDecimalBean beanInt = deserialize("{'value': 1}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1), beanInt.value);

// Long
BigDecimalBean beanLong = deserialize("{'value': 1234567890123}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1234567890123L), beanLong.value);

// Double
BigDecimalBean beanDouble = deserialize("{'value': 1.1}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1.1), beanDouble.value);

// Boolean
try {
deserialize("{'value': true}", BigDecimalBean.class);
fail("Should throw");
} catch (RuntimeException e) { // ignore
}

// String
try {
deserialize("{'value': 'foo'}", DoubleBean.class);
fail("Should throw");
} catch (RuntimeException e) { // ignore
}
}

@Test
public void primitiveDeserializeFloat() {
FloatBean beanFloat = deserialize("{'value': 1.1}", FloatBean.class);
Expand Down