Skip to content

Commit 27724a6

Browse files
authored
fix getBigDecimal with Boolean, for issue #2745 (#2746)
* fix #2745, support Boolean for getInteger, getBigInteger and getBigDecimal.
1 parent b7825ef commit 27724a6

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ public Integer getInteger(int index) {
490490
return Integer.parseInt(str);
491491
}
492492

493+
if (value instanceof Boolean) {
494+
return (boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
495+
}
496+
493497
throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
494498
}
495499

@@ -768,6 +772,10 @@ public BigInteger getBigInteger(int index) {
768772
return new BigInteger(str);
769773
}
770774

775+
if (value instanceof Boolean) {
776+
return (boolean) value ? BigInteger.ONE : BigInteger.ZERO;
777+
}
778+
771779
throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
772780
}
773781

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ public Integer getInteger(String key) {
620620
return Integer.parseInt(str);
621621
}
622622

623+
if (value instanceof Boolean) {
624+
return (boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
625+
}
626+
623627
throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
624628
}
625629

@@ -978,6 +982,10 @@ public BigInteger getBigInteger(String key) {
978982
return new BigInteger(str);
979983
}
980984

985+
if (value instanceof Boolean) {
986+
return (boolean) value ? BigInteger.ONE : BigInteger.ZERO;
987+
}
988+
981989
throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
982990
}
983991

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.alibaba.fastjson2.issues_2700;
2+
3+
import com.alibaba.fastjson2.JSONArray;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class Issue2745 {
13+
@Test
14+
public void Test1() throws Exception {
15+
JSONArray jsonArray = new JSONArray();
16+
jsonArray.add(true);
17+
assertEquals(BigDecimal.ONE, jsonArray.getBigDecimal(0));
18+
assertEquals(BigInteger.ONE, jsonArray.getBigInteger(0));
19+
assertEquals(Integer.valueOf(1), jsonArray.getInteger(0));
20+
}
21+
22+
@Test
23+
public void Test2() throws Exception {
24+
JSONObject obj = new JSONObject();
25+
obj.put("bool", true);
26+
assertEquals(BigDecimal.ONE, obj.getBigDecimal("bool"));
27+
assertEquals(BigInteger.ONE, obj.getBigInteger("bool"));
28+
assertEquals(Integer.valueOf(1), obj.getInteger("bool"));
29+
}
30+
}

fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONArray.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ public BigDecimal getBigDecimal(int index) {
210210
return toBigDecimal(str);
211211
}
212212

213+
if (value instanceof Boolean) {
214+
return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
215+
}
216+
213217
throw new JSONException("Can not cast '" + value.getClass() + "' to BigDecimal");
214218
}
215219

@@ -261,6 +265,10 @@ public Integer getInteger(int index) {
261265
return Integer.parseInt(str);
262266
}
263267

268+
if (value instanceof Boolean) {
269+
return (Boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
270+
}
271+
264272
throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
265273
}
266274

@@ -650,6 +658,10 @@ public BigInteger getBigInteger(int index) {
650658
return new BigInteger(str);
651659
}
652660

661+
if (value instanceof Boolean) {
662+
return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
663+
}
664+
653665
throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
654666
}
655667

fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONObject.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ public Integer getInteger(String key) {
525525
return Integer.parseInt(str);
526526
}
527527

528+
if (value instanceof Boolean) {
529+
return (Boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
530+
}
531+
528532
throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
529533
}
530534

@@ -756,6 +760,10 @@ public BigDecimal getBigDecimal(String key) {
756760
return toBigDecimal((String) value);
757761
}
758762

763+
if (value instanceof Boolean) {
764+
return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
765+
}
766+
759767
throw new JSONException("Can not cast '" + value.getClass() + "' to BigDecimal");
760768
}
761769

@@ -789,6 +797,10 @@ public BigInteger getBigInteger(String key) {
789797
return new BigInteger(str);
790798
}
791799

800+
if (value instanceof Boolean) {
801+
return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
802+
}
803+
792804
throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
793805
}
794806

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.alibaba.fastjson.issues_compatible;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.math.BigDecimal;
8+
import java.math.BigInteger;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class Issue2745 {
13+
@Test
14+
public void Test1() throws Exception {
15+
JSONArray jsonArray = new JSONArray();
16+
jsonArray.add(true);
17+
assertEquals(BigDecimal.ONE, jsonArray.getBigDecimal(0));
18+
assertEquals(BigInteger.ONE, jsonArray.getBigInteger(0));
19+
assertEquals(Integer.valueOf(1), jsonArray.getInteger(0));
20+
}
21+
22+
@Test
23+
public void Test2() throws Exception {
24+
JSONObject obj = new JSONObject();
25+
obj.put("bool", true);
26+
assertEquals(BigDecimal.ONE, obj.getBigDecimal("bool"));
27+
assertEquals(BigInteger.ONE, obj.getBigInteger("bool"));
28+
assertEquals(Integer.valueOf(1), obj.getInteger("bool"));
29+
}
30+
}

0 commit comments

Comments
 (0)