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 the incorrect double compare with e,E+,e+ #77

Merged
merged 1 commit into from
Dec 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ protected Number extractFloat() throws ParseException {

// follow JSonIJ parsing method
if (xs.length() > 18) {
BigDecimal big = new BigDecimal(xs);
// use extra CPU to check if the result can be return as double without precision lost
if (!unrestictBigDigit) {
double asDouble = Double.parseDouble(xs);
if (String.valueOf(asDouble).equals(xs))
final String doubleStr = String.valueOf(asDouble);
// we need a compare compat `e` `E` `e+` `E+`
if (compareDoublePrecision(doubleStr, xs)){
return asDouble;
}
}
return big;
return new BigDecimal(xs);
}

return Double.parseDouble(xs);
Expand All @@ -165,6 +167,33 @@ protected Number extractFloat() throws ParseException {
}
}

private boolean compareDoublePrecision(String convert, String origin) {
final char[] charArray = convert.toCharArray();
final char[] originArray = origin.toCharArray();
if (charArray.length > originArray.length) {
return false;
}
int j = 0;
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] < '0' || charArray[i] > '9') {
if (originArray[j] >= '0' && originArray[j] <= '9') {
return false;
} else {
j++;
if (originArray[j] == '+') {
j++;
}
continue;
}
}
if (charArray[i] != originArray[j]) {
return false;
}
j++;
}
return j == originArray.length;
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.jupiter.api.Test;

public class TestBigDigitUnrestricted {
public static String[] VALID_DOUBLE_JSON = new String[] {"{\"v\":0.12345678912345678}"};
public static String[] VALID_DOUBLE_JSON = new String[] {"{\"v\":0.12345678912345678}", "\"v\":\"1.7976931348623157E308\"", "\"v\":\"1.7976931348623157E+308\"", "\"v\":\"1.7976931348623157e+308\""};

@Test
public void testRestrictedBigDigit() throws Exception {
Expand Down