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";
@ -60,10 +58,10 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
this(); this();
this.parser = new JSONParser(in); this.parser = new JSONParser(in);
} }
public JSONObjectInputStream(InputStream in) { public JSONObjectInputStream(InputStream in) {
this(); this();
this.parser = new JSONParser(in); this.parser = new JSONParser(in);
} }
/** /**
@ -89,27 +87,47 @@ 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);
} finally { } finally {
objectCache.clear(); objectCache.clear();
} }
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())
return null; return null;
// See if the Object id is in the cache before continuing // See if the Object id is in the cache before continuing
if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt(MD_OBJECT_ID))) if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt(MD_OBJECT_ID)))
return objectCache.get(json.getInt(MD_OBJECT_ID)); return objectCache.get(json.getInt(MD_OBJECT_ID));
// Resolve the class // Resolve the class
Object obj = null; Object obj = null;
@ -148,7 +166,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
} }
// Add object to the cache // Add object to the cache
if(json.getString(MD_OBJECT_ID) != null) if(json.getString(MD_OBJECT_ID) != null)
objectCache.put(json.getInt(MD_OBJECT_ID), obj); objectCache.put(json.getInt(MD_OBJECT_ID), obj);
return obj; return obj;
} }
@ -170,7 +188,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
} }
} }
else if(List.class.isAssignableFrom(type)){ else if(List.class.isAssignableFrom(type)){
List list = (List)type.newInstance(); List list = (List)type.newInstance();
for(int i=0; i<json.size(); i++){ for(int i=0; i<json.size(); i++){
list.add(readObject(null, key, json.get(i))); list.add(readObject(null, key, json.get(i)));
} }
@ -181,39 +199,29 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
for(Iterator<String> it=json.keyIterator(); it.hasNext();){ for(Iterator<String> it=json.keyIterator(); it.hasNext();){
String subKey = it.next(); String subKey = it.next();
map.put( map.put(
subKey, subKey,
readObject(null, subKey, json.get(subKey))); readObject(null, subKey, json.get(subKey)));
} }
return map; return map;
} }
// 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 ||
type == Integer.class) return json.getInt(); type == Integer.class) return json.getInt();
else if(type == long.class || else if(type == long.class ||
type == Long.class) return json.getLong(); type == Long.class) return json.getLong();
else if(type == double.class || else if(type == double.class ||
type == Double.class) return json.getDouble(); type == Double.class) return json.getDouble();
else if(type == boolean.class || else if(type == boolean.class ||
type == Boolean.class) return json.getBoolean(); type == Boolean.class) return json.getBoolean();
else if(type == String.class) return json.getString(); else if(type == String.class) return json.getString();
return null; return null;
} }
@ -222,52 +230,52 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
@Override public void readFully(byte[] b) throws IOException { @Override public void readFully(byte[] b) throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public void readFully(byte[] b, int off, int len) throws IOException { @Override public void readFully(byte[] b, int off, int len) throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public int skipBytes(int n) throws IOException { @Override public int skipBytes(int n) throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public boolean readBoolean() throws IOException { @Override public boolean readBoolean() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public byte readByte() throws IOException { @Override public byte readByte() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public int readUnsignedByte() throws IOException { @Override public int readUnsignedByte() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public short readShort() throws IOException { @Override public short readShort() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public int readUnsignedShort() throws IOException { @Override public int readUnsignedShort() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public char readChar() throws IOException { @Override public char readChar() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public int readInt() throws IOException { @Override public int readInt() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public long readLong() throws IOException { @Override public long readLong() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public float readFloat() throws IOException { @Override public float readFloat() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public double readDouble() throws IOException { @Override public double readDouble() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public String readLine() throws IOException { @Override public String readLine() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public String readUTF() throws IOException { @Override public String readUTF() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override public int read() throws IOException { @Override public int read() throws IOException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }