Skip to content

Commit

Permalink
fix replace Collections.EmptyList with ArrayList for issue #1835
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Sep 6, 2023
1 parent 1648934 commit afac453
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Object createInstance(Collection collection) {
int size = collection.size();

if (size == 0 && (listClass == List.class)) {
Collection list = Collections.emptyList();
Collection list = new ArrayList();
if (builder != null) {
return builder.apply(list);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.alibaba.fastjson2.issues_1800;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class Issue1835 {
@Test
public void test() {
List<Person> sourceList = new ArrayList<>();
Set<Person> sourceSet = new HashSet<>();
Map<Integer, Person> sourceMap = new HashMap<>();
Map<String, Object> source = new HashMap<>();
source.put("data", sourceList);
source.put("data2", sourceSet);
source.put("data3", sourceMap);

JSONObject target = JSON.parseObject(JSON.toJSONString(source));
List<Person> targetList = target.getObject("data", getType("list"));
Set<Person> targetSet = target.getObject("data2", getType("set"));
Map<Integer, Person> targetMap = target.getObject("data3", getType("map"));

Person lisi = new Person("lisi");
targetList.add(lisi);
assertEquals("[{\"name\":\"lisi\"}]", JSON.toJSONString(targetList));
targetSet.add(lisi);
assertEquals("[{\"name\":\"lisi\"}]", JSON.toJSONString(targetSet));
targetMap.put(0, lisi);
assertEquals("{0:{\"name\":\"lisi\"}}", JSON.toJSONString(targetMap));
}

public static Type getType(String type) {
try {
Issue1835 app = new Issue1835();
Method method = app.getClass().getMethod(type);
return method.getGenericReturnType();
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public List<Person> list() {
return new ArrayList<>();
}

public Set<Person> set() {
return new HashSet<>();
}

public Map<Integer, Person> map() {
return new HashMap<>();
}

public static class Person {
private String name;

public Person() {
}

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}

0 comments on commit afac453

Please sign in to comment.