Bugfix and small update to JSONObjectInputStream

This commit is contained in:
Ziver Koc 2015-10-09 15:43:48 +00:00
parent 73868960f4
commit 267d5c6270

View file

@ -25,16 +25,13 @@
package zutil.parser.json; package zutil.parser.json;
import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import zutil.ClassUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.parser.Base64Decoder; import zutil.parser.Base64Decoder;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import javax.activation.UnsupportedDataTypeException; import javax.activation.UnsupportedDataTypeException;
import java.io.*; import java.io.*;
import java.lang.reflect.Array; import java.lang.reflect.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -42,6 +39,7 @@ import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{ public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
protected static final String MD_OBJECT_ID = "@object_id"; protected static final String MD_OBJECT_ID = "@object_id";
@ -89,11 +87,30 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
} }
public synchronized Object readObject() throws IOException { /**
* @return the object read from the stream
*/
@Override
public Object readObject() throws IOException {
return readObject(null);
}
/**
* @param <T> is a simple cast to this type
* @return the object read from the stream
*/
public <T> T readGenericObject() throws IOException {
return readObject(null);
}
/**
* @param c will override the registered root class and use this value instead
* @param <T>
* @return the object read from the stream
*/
public synchronized <T> T readObject(Class<T> c) throws IOException {
try{ try{
DataNode root = parser.read(); DataNode root = parser.read();
if(root != null){ if(root != null){
return readObject(null, null, root); return (T)readObject(c, null, root);
} }
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.WARNING, null, e); logger.log(Level.WARNING, null, e);
@ -103,6 +120,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
return null; return null;
} }
protected Object readObject(Class<?> type, String key, DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException, NoSuchFieldException { protected Object readObject(Class<?> type, String key, DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException, NoSuchFieldException {
// Only parse if json is a map // Only parse if json is a map
if(!json.isMap()) if(!json.isMap())
@ -188,20 +206,10 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
} }
// Field is a new Object // Field is a new Object
else{ else{
Field field = getFieldInClass(type, key); return readObject(type, key, json);
if(field != null)
return readObject(field.getType(), key, json);
else
return readObject(null, key, json);
} }
} }
private Field getFieldInClass(Class<?> c, String name){
for(Field f : c.getFields()){
if(f.getName().equals(name))
return f;
}
return null;
}
protected static Object readPrimitive(Class<?> type, DataNode json){ protected static Object readPrimitive(Class<?> type, DataNode json){
if (type == int.class || if (type == int.class ||