Skip to content

Commit

Permalink
add bigdecimal cast to int/long support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 21, 2019
1 parent 685fbd3 commit f1e51f5
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,13 @@ public static final Date castToDate(Object value) {
long longValue = -1;

if (value instanceof BigDecimal) {
longValue = ((BigDecimal) value).longValueExact();
BigDecimal decimal = (BigDecimal) value;
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
longValue = decimal.longValue();
} else {
longValue = decimal.longValueExact();
}
} else if (value instanceof Number) {
longValue = ((Number) value).longValue();
} else if (value instanceof String) {
Expand Down Expand Up @@ -401,7 +407,13 @@ public static final Long castToLong(Object value) {
}

if (value instanceof BigDecimal) {
return ((BigDecimal) value).longValueExact();
BigDecimal decimal = (BigDecimal) value;
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.longValue();
}

return decimal.longValueExact();
}

if (value instanceof Number) {
Expand Down Expand Up @@ -446,7 +458,14 @@ public static final Integer castToInt(Object value) {
}

if (value instanceof BigDecimal) {
return ((BigDecimal) value).intValueExact();
BigDecimal decimal = (BigDecimal) value;

int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.intValue();
}

return decimal.intValueExact();
}

if (value instanceof Number) {
Expand Down Expand Up @@ -673,7 +692,7 @@ public static final <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig ma
} else {
return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
}
} else if (obj instanceof Number) {
} else if (obj instanceof Integer || obj instanceof Long) {
int ordinal = ((Number) obj).intValue();
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
Expand Down Expand Up @@ -726,9 +745,11 @@ public static final <T> T cast(Object obj, ParameterizedType type, ParserConfig

if (obj instanceof List) {
List listObj = (List) obj;
ArrayList arrayList = new ArrayList(listObj.size());

for (int i = 0; i < listObj.size(); i++) {
int listObjSize = listObj.size();
ArrayList arrayList = new ArrayList(listObjSize);

for (int i = 0; i < listObjSize; i++) {
Object item = listObj.get(i);

Object itemValue;
Expand Down Expand Up @@ -839,6 +860,8 @@ public static final <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz
Number value = (Number) map.get("lineNumber");
if (value == null) {
lineNumber = 0;
} else if (value instanceof BigDecimal) {
lineNumber = ((BigDecimal) value).intValueExact();
} else {
lineNumber = value.intValue();
}
Expand Down Expand Up @@ -1750,7 +1773,7 @@ public static boolean getArgument(Type[] typeArgs, TypeVariable[] typeVariables,

public static double parseDouble(String str) {
final int len = str.length();
if (len >= 10) {
if (len > 10) {
return Double.parseDouble(str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public void test_0() throws Exception {
}
}

public void test_0_d() throws Exception {
Random r = new Random();

for (int i = 0; i < 1000 * 1000; ++i) {
String str = Double.toString(r.nextDouble());
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str));
}
}


public void test_1() throws Exception {
Random r = new Random();

Expand Down Expand Up @@ -44,6 +54,7 @@ public void test_3() throws Exception {

public void test_4() throws Exception {
String[] array = new String[] {
"0.34856254",
"1",
"12",
"123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void test_3() throws Exception {

public void test_4() throws Exception {
String[] array = new String[] {
"0.34856254",
"1",
"12",
"123",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.json.bvt.parser.number;

import com.alibaba.fastjson.JSONObject;
import junit.framework.TestCase;

import java.math.BigDecimal;

public class NumberValueTest_cast extends TestCase {
public void test_0() throws Exception {
JSONObject object = new JSONObject();
object.put("val", new BigDecimal("23.4"));

assertEquals(23, object.getByteValue("val"));
assertEquals(23, object.getShortValue("val"));
assertEquals(23, object.getIntValue("val"));
assertEquals(23, object.getLongValue("val"));
}
}

0 comments on commit f1e51f5

Please sign in to comment.