sync some APIs between JSONObject and JSONArray (#3811)
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (11, map[arch:arm64 os:macos-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (11, map[arch:arm64 os:ubuntu-24.04-arm]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (11, map[arch:x64 os:ubuntu-24.04]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (11, map[arch:x64 os:windows-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (17, map[arch:arm64 os:macos-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (17, map[arch:arm64 os:ubuntu-24.04-arm]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (17, map[arch:x64 os:ubuntu-24.04]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (17, map[arch:x64 os:windows-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (21, map[arch:arm64 os:macos-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (21, map[arch:arm64 os:ubuntu-24.04-arm]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (21, map[arch:x64 os:ubuntu-24.04]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (21, map[arch:x64 os:windows-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (25, map[arch:arm64 os:macos-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (25, map[arch:arm64 os:ubuntu-24.04-arm]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (25, map[arch:x64 os:ubuntu-24.04]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (25, map[arch:x64 os:windows-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (8, map[arch:arm64 os:macos-latest]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (8, map[arch:arm64 os:ubuntu-24.04-arm]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (8, map[arch:x64 os:ubuntu-24.04]) (push) Waiting to run Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os-arch.os }} (${{ matrix.os-arch.arch }}) (8, map[arch:x64 os:windows-latest]) (push) Waiting to run Details

* sync some APIs between JSONObject and JSONArray

* update getDate()
This commit is contained in:
jujn 2025-10-04 16:16:24 +08:00 committed by GitHub
parent e0f19121ca
commit 62c2cc9f46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 159 deletions

View File

@ -282,7 +282,7 @@ public class JSONArray
return Double.parseDouble(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Double");
throw new JSONException("Can not cast '" + value.getClass() + "' to double");
}
/**
@ -295,27 +295,8 @@ public class JSONArray
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
*/
public double getDoubleValue(int index) {
Object value = get(index);
if (value == null) {
return 0D;
}
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0D;
}
return Double.parseDouble(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to double value");
Double value = getDouble(index);
return value == null ? 0D : value;
}
/**
@ -352,7 +333,7 @@ public class JSONArray
return Float.parseFloat(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Float");
throw new JSONException("Can not cast '" + value.getClass() + "' to float");
}
/**
@ -365,27 +346,8 @@ public class JSONArray
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
*/
public float getFloatValue(int index) {
Object value = get(index);
if (value == null) {
return 0F;
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0F;
}
return Float.parseFloat(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to float value");
Float value = getFloat(index);
return value == null ? 0F : value;
}
/**
@ -418,6 +380,10 @@ public class JSONArray
return null;
}
if (str.indexOf('.') != -1) {
return (long) Double.parseDouble(str);
}
return Long.parseLong(str);
}
@ -455,6 +421,10 @@ public class JSONArray
return 0;
}
if (str.indexOf('.') != -1) {
return (long) Double.parseDouble(str);
}
return Long.parseLong(str);
}
@ -491,6 +461,10 @@ public class JSONArray
return null;
}
if (str.indexOf('.') != -1) {
return (int) Double.parseDouble(str);
}
return Integer.parseInt(str);
}
@ -528,6 +502,10 @@ public class JSONArray
return 0;
}
if (str.indexOf('.') != -1) {
return (int) Double.parseDouble(str);
}
return Integer.parseInt(str);
}
@ -568,7 +546,7 @@ public class JSONArray
return Short.parseShort(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Short");
throw new JSONException("Can not cast '" + value.getClass() + "' to short");
}
/**
@ -581,27 +559,8 @@ public class JSONArray
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
*/
public short getShortValue(int index) {
Object value = get(index);
if (value == null) {
return 0;
}
if (value instanceof Number) {
return ((Number) value).shortValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}
return Short.parseShort(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to short value");
Short value = getShort(index);
return value == null ? 0 : value;
}
/**
@ -634,7 +593,7 @@ public class JSONArray
return Byte.parseByte(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Byte");
throw new JSONException("Can not cast '" + value.getClass() + "' to byte");
}
/**
@ -647,27 +606,8 @@ public class JSONArray
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
*/
public byte getByteValue(int index) {
Object value = get(index);
if (value == null) {
return 0;
}
if (value instanceof Number) {
return ((Number) value).byteValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
}
return Byte.parseByte(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to byte value");
Byte value = getByte(index);
return value == null ? 0 : value;
}
/**
@ -703,7 +643,7 @@ public class JSONArray
return "true".equalsIgnoreCase(str) || "1".equals(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Boolean");
throw new JSONException("Can not cast '" + value.getClass() + "' to boolean");
}
/**
@ -715,26 +655,8 @@ public class JSONArray
* @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= size())}
*/
public boolean getBooleanValue(int index) {
Object value = get(index);
if (value == null) {
return false;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
if (value instanceof Number) {
return ((Number) value).intValue() == 1;
}
if (value instanceof String) {
String str = (String) value;
return "true".equalsIgnoreCase(str) || "1".equals(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to boolean value");
Boolean value = getBoolean(index);
return value != null && value;
}
/**
@ -851,6 +773,10 @@ public class JSONArray
return (Date) value;
}
if (value instanceof String) {
return DateUtils.parseDate((String) value);
}
if (value instanceof Number) {
long millis = ((Number) value).longValue();
if (millis == 0) {

View File

@ -407,7 +407,7 @@ public class JSONObject
return Double.parseDouble(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Double");
throw new JSONException("Can not cast '" + value.getClass() + "' to double");
}
/**
@ -419,27 +419,8 @@ public class JSONObject
* @throws JSONException Unsupported type conversion to double value
*/
public double getDoubleValue(String key) {
Object value = super.get(key);
if (value == null) {
return 0D;
}
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0D;
}
return Double.parseDouble(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to double value");
Double value = getDouble(key);
return value == null ? 0D : value;
}
/**
@ -475,7 +456,7 @@ public class JSONObject
return Float.parseFloat(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to Float");
throw new JSONException("Can not cast '" + value.getClass() + "' to float");
}
/**
@ -487,27 +468,8 @@ public class JSONObject
* @throws JSONException Unsupported type conversion to float value
*/
public float getFloatValue(String key) {
Object value = super.get(key);
if (value == null) {
return 0F;
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
String str = ((String) value).trim();
if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0F;
}
return Float.parseFloat(str);
}
throw new JSONException("Can not cast '" + value.getClass() + "' to float value");
Float value = getFloat(key);
return value == null ? 0F : value;
}
/**
@ -881,11 +843,11 @@ public class JSONObject
return null;
}
if (value instanceof Number) {
if (value instanceof BigInteger) {
return (BigInteger) value;
}
if (value instanceof Number) {
if (value instanceof BigDecimal) {
return ((BigDecimal) value).toBigInteger();
}
@ -950,8 +912,7 @@ public class JSONObject
}
if (value instanceof String) {
String str = ((String) value).trim();
return toBigDecimal(str);
return toBigDecimal(((String) value).trim());
}
if (value instanceof Boolean) {