Skip to content

Commit e1d26f5

Browse files
authored
Merge pull request #77 from Shoothzj/master
Fix the incorrect double compare with e,E+,e+
2 parents e87af25 + f099a5f commit e1d26f5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ protected Number extractFloat() throws ParseException {
148148

149149
// follow JSonIJ parsing method
150150
if (xs.length() > 18) {
151-
BigDecimal big = new BigDecimal(xs);
152151
// use extra CPU to check if the result can be return as double without precision lost
153152
if (!unrestictBigDigit) {
154153
double asDouble = Double.parseDouble(xs);
155-
if (String.valueOf(asDouble).equals(xs))
154+
final String doubleStr = String.valueOf(asDouble);
155+
// we need a compare compat `e` `E` `e+` `E+`
156+
if (compareDoublePrecision(doubleStr, xs)){
156157
return asDouble;
158+
}
157159
}
158-
return big;
160+
return new BigDecimal(xs);
159161
}
160162

161163
return Double.parseDouble(xs);
@@ -165,6 +167,33 @@ protected Number extractFloat() throws ParseException {
165167
}
166168
}
167169

170+
private boolean compareDoublePrecision(String convert, String origin) {
171+
final char[] charArray = convert.toCharArray();
172+
final char[] originArray = origin.toCharArray();
173+
if (charArray.length > originArray.length) {
174+
return false;
175+
}
176+
int j = 0;
177+
for (int i = 0; i < charArray.length; i++) {
178+
if (charArray[i] < '0' || charArray[i] > '9') {
179+
if (originArray[j] >= '0' && originArray[j] <= '9') {
180+
return false;
181+
} else {
182+
j++;
183+
if (originArray[j] == '+') {
184+
j++;
185+
}
186+
continue;
187+
}
188+
}
189+
if (charArray[i] != originArray[j]) {
190+
return false;
191+
}
192+
j++;
193+
}
194+
return j == originArray.length;
195+
}
196+
168197
/**
169198
* use to return Primitive Type, or String, Or JsonObject or JsonArray
170199
* generated by a ContainerFactory

json-smart/src/test/java/net/minidev/json/test/TestBigDigitUnrestricted.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.junit.jupiter.api.Test;
1111

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

1515
@Test
1616
public void testRestrictedBigDigit() throws Exception {

0 commit comments

Comments
 (0)