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

fix getBigDecimal with Boolean, for issue #2745 #2746

Merged
merged 2 commits into from
Jun 29, 2024
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
8 changes: 8 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ public Integer getInteger(int index) {
return Integer.parseInt(str);
}

if (value instanceof Boolean) {
return (boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
}

Expand Down Expand Up @@ -768,6 +772,10 @@ public BigInteger getBigInteger(int index) {
return new BigInteger(str);
}

if (value instanceof Boolean) {
return (boolean) value ? BigInteger.ONE : BigInteger.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
}

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ public Integer getInteger(String key) {
return Integer.parseInt(str);
}

if (value instanceof Boolean) {
return (boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
}

Expand Down Expand Up @@ -978,6 +982,10 @@ public BigInteger getBigInteger(String key) {
return new BigInteger(str);
}

if (value instanceof Boolean) {
return (boolean) value ? BigInteger.ONE : BigInteger.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.issues_2700;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2745 {
@Test
public void Test1() throws Exception {
JSONArray jsonArray = new JSONArray();
jsonArray.add(true);
assertEquals(BigDecimal.ONE, jsonArray.getBigDecimal(0));
assertEquals(BigInteger.ONE, jsonArray.getBigInteger(0));
assertEquals(Integer.valueOf(1), jsonArray.getInteger(0));
}

@Test
public void Test2() throws Exception {
JSONObject obj = new JSONObject();
obj.put("bool", true);
assertEquals(BigDecimal.ONE, obj.getBigDecimal("bool"));
assertEquals(BigInteger.ONE, obj.getBigInteger("bool"));
assertEquals(Integer.valueOf(1), obj.getInteger("bool"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public BigDecimal getBigDecimal(int index) {
return toBigDecimal(str);
}

if (value instanceof Boolean) {
return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigDecimal");
}

Expand Down Expand Up @@ -261,6 +265,10 @@ public Integer getInteger(int index) {
return Integer.parseInt(str);
}

if (value instanceof Boolean) {
return (Boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
}

Expand Down Expand Up @@ -650,6 +658,10 @@ public BigInteger getBigInteger(int index) {
return new BigInteger(str);
}

if (value instanceof Boolean) {
return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ public Integer getInteger(String key) {
return Integer.parseInt(str);
}

if (value instanceof Boolean) {
return (Boolean) value ? Integer.valueOf(1) : Integer.valueOf(0);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to Integer");
}

Expand Down Expand Up @@ -756,6 +760,10 @@ public BigDecimal getBigDecimal(String key) {
return toBigDecimal((String) value);
}

if (value instanceof Boolean) {
return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigDecimal");
}

Expand Down Expand Up @@ -789,6 +797,10 @@ public BigInteger getBigInteger(String key) {
return new BigInteger(str);
}

if (value instanceof Boolean) {
return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
}

throw new JSONException("Can not cast '" + value.getClass() + "' to BigInteger");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson.issues_compatible;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2745 {
@Test
public void Test1() throws Exception {
JSONArray jsonArray = new JSONArray();
jsonArray.add(true);
assertEquals(BigDecimal.ONE, jsonArray.getBigDecimal(0));
assertEquals(BigInteger.ONE, jsonArray.getBigInteger(0));
assertEquals(Integer.valueOf(1), jsonArray.getInteger(0));
}

@Test
public void Test2() throws Exception {
JSONObject obj = new JSONObject();
obj.put("bool", true);
assertEquals(BigDecimal.ONE, obj.getBigDecimal("bool"));
assertEquals(BigInteger.ONE, obj.getBigInteger("bool"));
assertEquals(Integer.valueOf(1), obj.getInteger("bool"));
}
}
Loading