Skip to content

Commit

Permalink
Merge pull request alibaba#36 from kraity/main
Browse files Browse the repository at this point in the history
Fixed and optimized JSONArray and JSONObject
  • Loading branch information
wenshao authored Apr 21, 2022
2 parents 7d85555 + cf72621 commit 037a0cc
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 142 deletions.
122 changes: 61 additions & 61 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public JSONArray(Collection<?> collection) {

public JSONArray(Object... items) {
super(items.length);

for (Object item : items) {
add(item);
}
Expand Down Expand Up @@ -423,7 +422,7 @@ public Byte getByte(int index) {
throw new JSONException("can not cast " + value.getClass() + " to byte");
}

public short getByteValue(int index) {
public byte getByteValue(int index) {
Object value = get(index);

if (value == null) {
Expand Down Expand Up @@ -465,13 +464,7 @@ public boolean getBooleanValue(int index) {

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty()
|| str.equalsIgnoreCase("null")) {
return false;
}

return str.equalsIgnoreCase("true");
return str.equalsIgnoreCase("true") || str.equals("1");
}

throw new JSONException("can not convert to boolean : " + value);
Expand All @@ -494,13 +487,10 @@ public Boolean getBoolean(int index) {

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty()
|| str.equalsIgnoreCase("null")) {
if (str.isEmpty() || str.equalsIgnoreCase("null")) {
return null;
}

return str.equalsIgnoreCase("true");
return str.equalsIgnoreCase("true") || str.equals("1");
}

throw new JSONException("can not convert to boolean : " + value);
Expand All @@ -513,12 +503,17 @@ public BigInteger getBigInteger(int index) {
return null;
}

if (value instanceof BigInteger) {
return (BigInteger) value;
}
if (value instanceof Number) {
if (value instanceof BigInteger) {
return (BigInteger) value;
}

if (value instanceof BigDecimal) {
return ((BigDecimal) value).toBigInteger();
if (value instanceof BigDecimal) {
return ((BigDecimal) value).toBigInteger();
}

long longValue = ((Number) value).longValue();
return BigInteger.valueOf(longValue);
}

if (value instanceof String) {
Expand All @@ -530,17 +525,6 @@ public BigInteger getBigInteger(int index) {
return new BigInteger(str);
}

if (value instanceof Byte
|| value instanceof Short
|| value instanceof Integer
|| value instanceof Long
|| value instanceof Float
|| value instanceof Double
) {
long longValue = ((Number) value).longValue();
return BigInteger.valueOf(longValue);
}

throw new JSONException("can not cast " + value.getClass() + " to Long");
}

Expand All @@ -551,18 +535,21 @@ public BigDecimal getBigDecimal(int index) {
return null;
}

if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
if (value instanceof Number) {
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}

if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
}
if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
}

if (value instanceof Float
|| value instanceof Double) {
// Floating point number have no cached BigDecimal
return new BigDecimal(value.toString());
}

if (value instanceof Byte
|| value instanceof Short
|| value instanceof Integer
|| value instanceof Long) {
long longValue = ((Number) value).longValue();
return BigDecimal.valueOf(longValue);
}
Expand All @@ -576,12 +563,6 @@ public BigDecimal getBigDecimal(int index) {
return new BigDecimal(str);
}

if (value instanceof Float
|| value instanceof Double) {
double doubleValue = ((Number) value).doubleValue();
return BigDecimal.valueOf(doubleValue);
}

throw new JSONException("can not cast " + value.getClass() + " to Long");
}

Expand All @@ -598,19 +579,23 @@ public String getString(int index) {
return JSON.toJSONString(value);
}

public java.util.Date getDate(int index) {
public Date getDate(int index) {
Object value = get(index);

if (value == null || value instanceof Date) {
return (java.util.Date) value;
if (value == null) {
return null;
}

if (value instanceof Date) {
return (Date) value;
}

if (value instanceof Number) {
long millis = ((Number) value).longValue();
if (millis == 0) {
return null;
}
return new java.util.Date(millis);
return new Date(millis);
}

return TypeUtils.toDate(value);
Expand All @@ -619,7 +604,11 @@ public java.util.Date getDate(int index) {
public Instant getInstant(int index) {
Object value = get(index);

if (value == null || value instanceof Instant) {
if (value == null) {
return null;
}

if (value instanceof Instant) {
return (Instant) value;
}

Expand Down Expand Up @@ -659,6 +648,11 @@ public <T> List<T> toJavaList(Class<T> clazz) {
return list;
}

public String toJSONString() {
return toString();
}

@Override
public String toString() {
try (JSONWriter writer = JSONWriter.of()) {
if (arrayWriter == null) {
Expand All @@ -669,22 +663,28 @@ public String toString() {
}
}

public static JSONArray of(Object... items) {
return new JSONArray(items);
}

public static JSONArray of(Object item) {
return new JSONArray(1)
.fluentAdd(item);
JSONArray array = new JSONArray(1);
array.add(item);
return array;
}

public static JSONArray of(Object first, Object second) {
return new JSONArray(2)
.fluentAdd(first)
.fluentAdd(second);
JSONArray array = new JSONArray(2);
array.add(first);
array.add(second);
return array;
}

public static JSONArray of(Object... items) {
JSONArray array = new JSONArray(items.length);
for (Object item : items) {
array.add(item);
}
public static JSONArray of(Object first, Object second, Object third) {
JSONArray array = new JSONArray(3);
array.add(first);
array.add(second);
array.add(third);
return array;
}
}
Loading

0 comments on commit 037a0cc

Please sign in to comment.