Skip to content

Commit

Permalink
add testcase, alibaba#131
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 4, 2022
1 parent c8cfce1 commit 8d9ec37
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ static <T> T parseObject(String text, Type type) {
* @param typeReference specify the {@link TypeReference} to be converted
*/
@SuppressWarnings({"unchecked", "rawtypes"})
static <T> T parseObject(String text, TypeReference typeReference) {
static <T> T parseObject(String text, TypeReference typeReference, JSONReader.Feature ... features) {
if (text == null || text.isEmpty()) {
return null;
}

try (JSONReader reader = JSONReader.of(text)) {
reader.context.config(features);
Type type = typeReference.getType();
ObjectReader<T> objectReader = reader.context.provider.getObjectReader(type);
return objectReader.readObject(reader, 0);
Expand Down Expand Up @@ -636,14 +637,16 @@ static JSONArray parseArray(String text, JSONReader.Feature... features) {
*
* @param text the JSON {@link String} to be parsed
* @param type specify the {@link Type} to be converted
* @param features features to be enabled in parsing
*/
static <T> List<T> parseArray(String text, Type type) {
static <T> List<T> parseArray(String text, Type type, JSONReader.Feature... features) {
if (text == null || text.isEmpty()) {
return null;
}
ParameterizedTypeImpl paramType = new ParameterizedTypeImpl(new Type[]{type}, null, List.class);

try (JSONReader reader = JSONReader.of(text)) {
reader.context.config(features);
return reader.read(paramType);
}
}
Expand All @@ -653,14 +656,17 @@ static <T> List<T> parseArray(String text, Type type) {
*
* @param text the JSON {@link String} to be parsed
* @param types specify some {@link Type}s to be converted
* @param features features to be enabled in parsing
*/
static <T> List<T> parseArray(String text, Type[] types) {
static <T> List<T> parseArray(String text, Type[] types, JSONReader.Feature... features) {
if (text == null || text.isEmpty()) {
return null;
}
List<T> array = new ArrayList<>(types.length);

try (JSONReader reader = JSONReader.of(text)) {
reader.context.config(features);

reader.startArray();
for (Type itemType : types) {
array.add(
Expand Down
74 changes: 74 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,78 @@ public static class MyList<T> extends ArrayList<T> {
private static class MyList0<T> extends ArrayList<T> {

}

@Test
public void testNulls() {
assertNull(JSON.parse(null));
assertNull(JSON.parse(null, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((String) null));
assertNull(JSON.parseObject((String) null, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((byte[]) null));
assertNull(JSON.parseObject((byte[]) null, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject(new byte[0]));
assertNull(JSON.parseObject(new byte[0], JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((byte[]) null, User.class));
assertNull(JSON.parseObject((byte[]) null, User.class, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((byte[]) null, (Type) User.class));
assertNull(JSON.parseObject((byte[]) null, (Type) User.class, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((byte[]) null, (Type) User.class, ""));
assertNull(JSON.parseObject((byte[]) null, (Type) User.class, "", JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject(new byte[0], (Type) User.class, ""));
assertNull(JSON.parseObject(new byte[0], (Type) User.class, "", JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject(new byte[0], 0, 0, StandardCharsets.UTF_8, (Type) User.class));

assertNull(JSON.parseObject((String) null, User.class));
assertNull(JSON.parseObject((String) null, User.class, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((String) null, (Type) User.class));
assertNull(JSON.parseObject((String) null, (Type) User.class, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((String) null, User.class, ""));
assertNull(JSON.parseObject((String) null, User.class, "", JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((String) null, (Type) User.class, ""));
assertNull(JSON.parseObject((String) null, (Type) User.class, "", JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseObject((String) null, new TypeReference<List<User>>(){}));
assertNull(JSON.parseObject((String) null, new TypeReference<List<User>>(){}, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseArray((String) null));
assertNull(JSON.parseArray((String) null, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseArray(""));
assertNull(JSON.parseArray("", JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseArray((String) null));
assertNull(JSON.parseArray((String) null, JSONReader.Feature.SupportAutoType));

assertNull(JSON.parseArray("", User.class));
assertNull(JSON.parseArray("", User.class, JSONReader.Feature.SupportAutoType));
assertNull(JSON.parseArray("", new Type[] {User.class}, JSONReader.Feature.SupportAutoType));

assertEquals("null", JSON.toJSONString(null, (Filter) null));
assertEquals("null", JSON.toJSONString(null, (Filter[]) null));
assertEquals("null", JSON.toJSONString(null, ""));
assertEquals("null", JSON.toJSONString(null, "", (Filter[]) null));

assertEquals("null", new String(JSON.toJSONBytes(null, (Filter) null)));
assertEquals("null", new String(JSON.toJSONBytes(null, (Filter[]) null)));
assertEquals("null", new String(JSON.toJSONBytes(null, "", (Filter[]) null)));

assertFalse(JSON.isValid((String) null));
assertFalse(JSON.isValid(""));
assertFalse(JSON.isValid((byte[]) null));

assertFalse(JSON.isValidArray((String) null));
assertFalse(JSON.isValidArray(""));
assertFalse(JSON.isValidArray((byte[]) null));
}
}

0 comments on commit 8d9ec37

Please sign in to comment.