Updated JSONObjectOutputStream to support extended generics

This commit is contained in:
Ziver Koc 2015-11-05 02:07:08 +01:00
parent 2dae3a6f01
commit 8ca2f21c7c
3 changed files with 54 additions and 13 deletions

View file

@ -129,8 +129,8 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object readType(Class<?> type, Class<?>[] genType, String key, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, UnsupportedDataTypeException, NoSuchFieldException {
if(json == null)
protected Object readType(Class<?> type, String key, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, UnsupportedDataTypeException, NoSuchFieldException {
if(json == null || type == null)
return null;
// Field type is a primitive?
if(type.isPrimitive() || String.class.isAssignableFrom(type)){
@ -142,25 +142,27 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
else{
Object array = Array.newInstance(type.getComponentType(), json.size());
for(int i=0; i<json.size(); i++){
Array.set(array, i, readType(type.getComponentType(), null, key, json.get(i)));
Array.set(array, i, readType(type.getComponentType(), key, json.get(i)));
}
return array;
}
}
else if(List.class.isAssignableFrom(type)){
Class<?>[] genType = ClassUtil.getGenericClasses(type, List.class);
List list = (List)type.newInstance();
for(int i=0; i<json.size(); i++){
list.add(readType(genType[0], null, key, json.get(i)));
list.add(readType((genType.length>=1? genType[0] : null), key, json.get(i)));
}
return list;
}
else if(Map.class.isAssignableFrom(type)){
Class<?>[] genType = ClassUtil.getGenericClasses(type, Map.class);
Map map = (Map)type.newInstance();
for(Iterator<String> it=json.keyIterator(); it.hasNext();){
String subKey = it.next();
map.put(
subKey,
readType(genType[1], null, subKey, json.get(subKey)));
readType((genType.length>=2? genType[1] : null), subKey, json.get(subKey)));
}
return map;
}
@ -210,7 +212,6 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
field.setAccessible(true);
field.set(obj, readType(
field.getType(),
ClassUtil.getGenericClasses(field),
field.getName(),
json.get(field.getName())));
}