mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix json double
This commit is contained in:
@@ -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@Gitee,Double会出现小数精度丢失问题,此处使用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;
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user