Skip to content

Commit 4cc05ab

Browse files
committed
support float/double/BigDecimal/BigInteger sum
1 parent 8cd3c3b commit 4cc05ab

3 files changed

Lines changed: 86 additions & 2 deletions

File tree

core/src/main/java/com/alibaba/fastjson2/JSONPathSegment.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.alibaba.fastjson2.writer.ObjectWriterProvider;
1212

1313
import java.lang.reflect.Array;
14+
import java.math.BigDecimal;
15+
import java.math.BigInteger;
1416
import java.util.*;
1517
import java.util.function.BiConsumer;
1618
import java.util.function.BiFunction;
@@ -1745,6 +1747,21 @@ static Number add(Number a, Number b) {
17451747
if (aIsInt && bIsInt) {
17461748
return a.longValue() + b.longValue();
17471749
}
1750+
1751+
boolean aIsDouble = a instanceof Float || a instanceof Double;
1752+
boolean bIsDouble = b instanceof Float || b instanceof Double;
1753+
if (aIsDouble || bIsDouble) {
1754+
return a.doubleValue() + b.doubleValue();
1755+
}
1756+
1757+
if (a instanceof BigDecimal || b instanceof BigDecimal) {
1758+
return TypeUtils.toBigDecimal(a).add(TypeUtils.toBigDecimal(b));
1759+
}
1760+
1761+
if (a instanceof BigInteger || b instanceof BigInteger) {
1762+
return TypeUtils.toBigInteger(a).add(TypeUtils.toBigInteger(b));
1763+
}
1764+
17481765
throw new JSONException("not support operation");
17491766
}
17501767

core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterImplDouble.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type f
3131
return;
3232
}
3333

34-
if (format != null) {
35-
String str = format.format(object);
34+
DecimalFormat decimalFormat = this.format;
35+
if (decimalFormat == null) {
36+
String format = jsonWriter.getContext().getDateFormat();
37+
if (format != null && format.indexOf("#") != -1) {
38+
decimalFormat = new DecimalFormat(format);
39+
}
40+
}
41+
42+
if (decimalFormat != null) {
43+
String str = decimalFormat.format(object);
3644
jsonWriter.writeRaw(str);
3745
return;
3846
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.alibaba.fastjson2.issues_2000;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONArray;
5+
import com.alibaba.fastjson2.JSONObject;
6+
import com.alibaba.fastjson2.JSONPath;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class Issue2065 {
15+
@Test
16+
public void multiClassSum() {
17+
JSONObject json = new JSONObject();
18+
JSONArray jsonArr = new JSONArray();
19+
jsonArr.add(new JSONObject().fluentPut("price", 10L));
20+
jsonArr.add(new JSONObject().fluentPut("price", 10.1f));
21+
jsonArr.add(new JSONObject().fluentPut("price", 10.1d));
22+
jsonArr.add(new JSONObject().fluentPut("price", BigInteger.valueOf(10L)));
23+
jsonArr.add(new JSONObject().fluentPut("price", BigDecimal.valueOf(10.1d)));
24+
25+
json.put("arr", jsonArr);
26+
Object o = JSONPath.eval(json, "$.arr[*].price.sum()");
27+
28+
assertEquals(JSON.toJSONString(o, "##.#"), "50.3");
29+
}
30+
31+
@Test
32+
public void multiClassSum1() {
33+
JSONObject json = new JSONObject();
34+
JSONArray jsonArr = new JSONArray();
35+
jsonArr.add(new JSONObject().fluentPut("price", 10L));
36+
jsonArr.add(new JSONObject().fluentPut("price", BigInteger.valueOf(10L)));
37+
38+
json.put("arr", jsonArr);
39+
// 这一步eval会提示 not support operation 。
40+
// 原因是 com.alibaba.fastjson2.JSONPathSegment$SumSegment#add方法类型判断不够全面,无法支持Float,Double,BigInteger,BigDecimal
41+
Object o = JSONPath.eval(json, "$.arr[*].price.sum()");
42+
43+
assertEquals(JSON.toJSONString(o, "##.#"), "20");
44+
}
45+
46+
@Test
47+
public void integerSum() {
48+
JSONObject json = new JSONObject();
49+
JSONArray jsonArr = new JSONArray();
50+
jsonArr.add(new JSONObject().fluentPut("price", 10L));
51+
jsonArr.add(new JSONObject().fluentPut("price", 10L));
52+
53+
json.put("arr", jsonArr);
54+
//由于此时 price都是long类型sum正常
55+
Object o = JSONPath.eval(json, "$.arr[*].price.sum()");
56+
57+
assertEquals(o.toString(), "20");
58+
}
59+
}

0 commit comments

Comments
 (0)