Skip to content

Commit

Permalink
getLong support boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 9, 2024
1 parent 4f9d999 commit 72fae17
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ public Long getLong(int index) {
return Long.parseLong(str);
}

if (value instanceof Boolean) {
return (boolean) value ? Long.valueOf(1) : Long.valueOf(0);
}

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

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ public Long getLong(String key) {
return Long.parseLong(str);
}

if (value instanceof Boolean) {
return (boolean) value ? Long.valueOf(1) : Long.valueOf(0);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void Test1() throws Exception {
assertEquals(BigDecimal.ONE, jsonArray.getBigDecimal(0));
assertEquals(BigInteger.ONE, jsonArray.getBigInteger(0));
assertEquals(Integer.valueOf(1), jsonArray.getInteger(0));
assertEquals(Long.valueOf(1), jsonArray.getLong(0));
}

@Test
Expand All @@ -26,5 +27,6 @@ public void Test2() throws Exception {
assertEquals(BigDecimal.ONE, obj.getBigDecimal("bool"));
assertEquals(BigInteger.ONE, obj.getBigInteger("bool"));
assertEquals(Integer.valueOf(1), obj.getInteger("bool"));
assertEquals(Long.valueOf(1), obj.getLong("bool"));
}
}

0 comments on commit 72fae17

Please sign in to comment.