Skip to content

Commit

Permalink
* create list, map instance automated
Browse files Browse the repository at this point in the history
  • Loading branch information
bes2008 committed Jul 19, 2019
1 parent c4a20ff commit ccce4f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.progsbase.libraries.JSON.JSONReflectiveWriter;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -39,13 +41,13 @@ public void testProgsbase() throws Exception {
// test list
String str2 = JSONReflectiveWriter.writeJSON(persons);
System.out.println(str2);
List<Person> persons2 = JSONReflectiveReader.readJSON(str2, List.class, new GenericTypeGetter<List<Person>>() {
List<Person> persons2 = JSONReflectiveReader.readJSON(str2, ArrayList.class, new GenericTypeGetter<List<Person>>() {
}.getType());
System.out.println(JSONReflectiveWriter.writeJSON(persons2));
// test map
String str3 = JSONReflectiveWriter.writeJSON(idToPersonMap);
System.out.println(str3);
Map<Integer, Person> personMap = JSONReflectiveReader.readJSON(str3, Map.class, new GenericTypeGetter<Map<String, Person>>() {
Map<Integer, Person> personMap = JSONReflectiveReader.readJSON(str3, HashMap.class, new GenericTypeGetter<Map<String, Person>>() {
}.getType());
System.out.println(JSONReflectiveWriter.writeJSON(personMap));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import com.github.fangjinuo.easyjson.core.JSONBuilderProvider;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

import static JSON.StringElementMaps.StringElementMaps.GetStringElementMapNumberOfKeys;

Expand Down Expand Up @@ -114,10 +113,19 @@ public static <T> T javaifyJSONValue(Element element, Class<T> clazz, Type gener
}

public static <T> T javaifyJSONObject(StringElementMap object, Class<T> clazz) throws JSONException {
T t;

T t = null;
if(clazz.isInterface()){
if(clazz == Map.class){
t = (T)new HashMap();
}
else if(clazz == Collection.class || clazz == List.class){
t = (T)new ArrayList();
}
}
try {
t = clazz.newInstance();
if(t == null) {
t = clazz.newInstance();
}
} catch (Throwable e) {
throw new JSONException(e);
}
Expand Down

0 comments on commit ccce4f8

Please sign in to comment.