Bugfix and small update to JSONObjectInputStream
This commit is contained in:
parent
73868960f4
commit
267d5c6270
1 changed files with 63 additions and 55 deletions
|
|
@ -25,16 +25,13 @@
|
|||
package zutil.parser.json;
|
||||
|
||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||
import zutil.ClassUtil;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.parser.Base64Decoder;
|
||||
import zutil.parser.DataNode;
|
||||
|
||||
import javax.activation.UnsupportedDataTypeException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
|
@ -42,6 +39,7 @@ import java.util.Map;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
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{
|
||||
DataNode root = parser.read();
|
||||
if(root != null){
|
||||
return readObject(null, null, root);
|
||||
return (T)readObject(c, null, root);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.WARNING, null, e);
|
||||
|
|
@ -103,6 +120,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected Object readObject(Class<?> type, String key, DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException, NoSuchFieldException {
|
||||
// Only parse if json is a map
|
||||
if(!json.isMap())
|
||||
|
|
@ -188,20 +206,10 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
|||
}
|
||||
// Field is a new Object
|
||||
else{
|
||||
Field field = getFieldInClass(type, key);
|
||||
if(field != null)
|
||||
return readObject(field.getType(), key, json);
|
||||
else
|
||||
return readObject(null, key, json);
|
||||
return readObject(type, 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){
|
||||
if (type == int.class ||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue