diff --git a/src/zutil/parser/json/JSONObjectInputStream.java b/src/zutil/parser/json/JSONObjectInputStream.java index 3f40460..f519b70 100755 --- a/src/zutil/parser/json/JSONObjectInputStream.java +++ b/src/zutil/parser/json/JSONObjectInputStream.java @@ -106,7 +106,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C return (T) readObject(c, null, root); } } catch (Exception e) { - logger.log(Level.WARNING, null, e); + logger.log(Level.SEVERE, null, e); } finally { objectCache.clear(); } @@ -115,11 +115,12 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C @SuppressWarnings({ "rawtypes", "unchecked" }) - protected Object readType(Class type, Class[] genericType, String key, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { - // TODO: Don't replace the existing object if it exists + protected Object readType(Class type, Class[] genericType, Object currentValue, String key, DataNode json) + throws IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException { if (json == null || type == null) return null; + // Field type is a primitive? if (ClassUtil.isPrimitive(type) || ClassUtil.isWrapper(type)) { @@ -134,7 +135,12 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C else { Object array = Array.newInstance(type.getComponentType(), json.size()); for (int i=0; i=1? genericType[0] : null), null, key, json.get(i))); + list.add(readType( + (genericType.length>=1? genericType[0] : null), + null, + null, + key, + json.get(i))); } return list; } else if (Map.class.isAssignableFrom(type)) { if (genericType == null || genericType.length < 2) genericType = ClassUtil.getGenericClasses(type, Map.class); - Map map = (Map) type.getDeclaredConstructor().newInstance(); + + Map map = (Map) currentValue; + if (map == null) { + if (type == Map.class) + map = new HashMap(); + else + map = (Map) type.getDeclaredConstructor().newInstance(); + } for (Iterator it=json.keyIterator(); it.hasNext();) { String subKey = it.next(); if (json.get(subKey) != null) { - map.put( + map.put(subKey, readType( + (genericType.length >= 2 ? genericType[1] : null), + null, + null, subKey, - readType((genericType.length >= 2 ? genericType[1] : null), null, subKey, json.get(subKey))); + json.get(subKey))); } } return map; @@ -208,10 +238,10 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C json.get(field.getName()) != null) { // Parse field field.setAccessible(true); - field.set(obj, - readType( + field.set(obj, readType( field.getType(), ClassUtil.getGenericClasses(field), + field.get(obj), field.getName(), json.get(field.getName()))); }