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

When using BrowserCompatible, BigDecimal in the Double range behaves the same as Double. for issue #3123 #3136

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,7 @@ protected static boolean isWriteAsString(BigDecimal value, long features) {
}

return (features & BrowserCompatible.mask) != 0
&& value.precision() >= 16
&& !isJavaScriptSupport(value.unscaledValue());
&& !isJavaScriptSupport(value);
}

public abstract void writeNameRaw(char[] chars);
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4347,8 +4347,23 @@ public static boolean isJavaScriptSupport(long i) {
return i >= LONG_JAVASCRIPT_LOW && i <= LONG_JAVASCRIPT_HIGH;
}

public static boolean isJavaScriptSupport(BigDecimal i) {
return i.precision() >= 16 && isJavaScriptSupport(i.unscaledValue());
public static boolean isJavaScriptSupport(BigDecimal decimal) {
boolean jsSupport = decimal.precision() < 16 || isJavaScriptSupport(decimal.unscaledValue());
if (!jsSupport && decimal.scale() != 0) {
//Use double for comparison
//double and javascript number have the same precision
//In extreme cases, precision loss may occur.
//There will be a loss of precision between [4.9e-324, 5e-324), which will be converted to 5e-324 by JavaScript.
//This situation can be ignored
double doubleValue;
try {
doubleValue = decimal.doubleValue();
} catch (Exception ex) {
return false;
}
jsSupport = decimal.compareTo(BigDecimal.valueOf(doubleValue)) == 0;
}
return jsSupport;
}

public static boolean isJavaScriptSupport(BigInteger i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alibaba.fastjson2.issues_3100;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
* @author 张治保
* @since 2024/10/22
*/
public class Issue3123 {
@Test
@SneakyThrows
void test() {
BigDecimal num1 = BigDecimal.valueOf(107.29305145106407);
BigDecimal num2 = BigDecimal.valueOf(23.42835970945431);
BigDecimal num3 = BigDecimal.valueOf(23.427479896686418);

Double num11 = 107.29305145106407;
Double num21 = 23.42835970945431;
Double num31 = 23.427479896686418;

Map map = new HashMap<>();
map.put("b1", num1);
map.put("b2", num2);
map.put("b3", num3);

map.put("d1", num11);
map.put("d2", num21);
map.put("d3", num31);

JSONAssert.assertEquals(
"{\"b1\":107.29305145106407,\"b2\":23.42835970945431,\"b3\":23.427479896686418,\"d1\":107.29305145106407,\"d2\":23.42835970945431,\"d3\":23.427479896686418}",
JSON.toJSONString(map, JSONWriter.Feature.BrowserCompatible),
true
);
}
}