fix json double

This commit is contained in:
Looly
2020-10-11 13:45:33 +08:00
parent 6ea9912fd4
commit c4fdf545e7
5 changed files with 95 additions and 78 deletions

View File

@@ -140,12 +140,14 @@ final class InternalJSONUtil {
* @return A simple JSON value.
*/
protected static Object stringToValue(String string) {
// null处理
if (null == string || "null".equalsIgnoreCase(string)) {
return JSONNull.NULL;
}
if (StrUtil.EMPTY.equals(string)) {
return string;
// boolean处理
if (0 == string.length()) {
return StrUtil.EMPTY;
}
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE;
@@ -154,17 +156,22 @@ final class InternalJSONUtil {
return Boolean.FALSE;
}
/* If it might be a number, try converting it. If a number cannot be produced, then the value will just be a string. */
// Number处理
char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '-') {
try {
if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1) {
if (StrUtil.containsAnyIgnoreCase(string, ".", "e")) {
// pr#192@GiteeDouble会出现小数精度丢失问题此处使用BigDecimal
//double d = Double.parseDouble(string);
//if (false == Double.isInfinite(d) && false == Double.isNaN(d)) {
// return d;
//}
return new BigDecimal(string);
} else {
Long myLong = new Long(string);
if (string.equals(myLong.toString())) {
if (myLong == myLong.intValue()) {
return myLong.intValue();
final long myLong = Long.parseLong(string);
if (string.equals(Long.toString(myLong))) {
if (myLong == (int) myLong) {
return (int) myLong;
} else {
return myLong;
}
@@ -173,6 +180,8 @@ final class InternalJSONUtil {
} catch (Exception ignore) {
}
}
// 其它情况返回原String值下
return string;
}

View File

@@ -349,7 +349,7 @@ public class JSONTokener {
this.back();
string = sb.toString().trim();
if ("".equals(string)) {
if (0 == string.length()) {
throw this.syntaxError("Missing value");
}
return InternalJSONUtil.stringToValue(string);