hal/src/zutil/parser/json/JSONObjectInputStream.java

241 lines
7.7 KiB
Java
Raw Normal View History

2015-05-27 13:13:19 +00:00
/*
* Copyright (c) 2015 ezivkoc
2013-06-06 22:36:00 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2015-05-27 13:13:19 +00:00
*/
2013-06-06 22:36:00 +00:00
package zutil.parser.json;
import zutil.parser.Base64Decoder;
import zutil.parser.DataNode;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
2013-07-31 16:22:51 +00:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2013-06-06 22:36:00 +00:00
2013-07-30 15:13:53 +00:00
import javax.activation.UnsupportedDataTypeException;
2013-06-06 22:36:00 +00:00
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
private JSONParser parser;
2013-08-02 14:49:16 +00:00
private HashMap<Integer, Object> objectCache;
public JSONObjectInputStream(Reader in) {
this.parser = new JSONParser(in);
2013-08-02 14:49:16 +00:00
this.objectCache = new HashMap<Integer, Object>();
}
2013-07-30 15:13:53 +00:00
public Object readObject() throws IOException {
try{
DataNode root = parser.read();
if(root != null){
2013-07-30 15:13:53 +00:00
return readObject(root);
}
2013-07-30 15:13:53 +00:00
// TODO: Fix Exceptions
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
2013-07-30 15:13:53 +00:00
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
2013-07-31 16:22:51 +00:00
} finally {
2013-08-02 14:49:16 +00:00
objectCache.clear();
2013-07-30 15:13:53 +00:00
}
return null;
}
2013-07-31 16:22:51 +00:00
protected Object readObject(DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException {
// See if the Object id is in the cache before continuing
2013-08-02 14:49:16 +00:00
if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt("@object_id")))
return objectCache.get(json.getInt("@object_id"));
2013-07-31 16:22:51 +00:00
Class<?> objClass = Class.forName(json.getString("@class"));
Object obj = objClass.newInstance();
// Read all fields from the new object instance
for(Field field : obj.getClass().getDeclaredFields()){
if((field.getModifiers() & Modifier.STATIC) == 0 &&
(field.getModifiers() & Modifier.TRANSIENT) == 0 &&
json.get(field.getName()) != null){
// Parse field
field.setAccessible(true);
field.set(obj, readValue(
2013-07-30 15:13:53 +00:00
field.getType(),
json.get(field.getName())));
}
}
2013-07-31 16:22:51 +00:00
// Add object to the cache
if(json.getString("@object_id") != null)
2013-08-02 14:49:16 +00:00
objectCache.put(json.getInt("@object_id"), obj);
return obj;
}
2013-07-30 15:13:53 +00:00
@SuppressWarnings({ "rawtypes", "unchecked" })
2013-07-31 16:22:51 +00:00
protected Object readValue(Class<?> type, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, UnsupportedDataTypeException {
// Field type is a primitive?
2013-07-30 15:13:53 +00:00
if(type.isPrimitive() || String.class.isAssignableFrom(type)){
return readPrimitive(type, json);
}
else if(type.isArray()){
if(type.getComponentType() == Byte.class)
return Base64Decoder.decodeToByte(json.getString());
else{
Object array = Array.newInstance(type.getComponentType(), json.size());
for(int i=0; i<json.size(); i++){
Array.set(array, i, readValue(type.getComponentType(), json.get(i)));
}
return array;
}
}
else if(List.class.isAssignableFrom(type)){
// TODO Add List Support
2013-07-30 15:13:53 +00:00
List list = (List)type.newInstance();
for(int i=0; i<json.size(); i++){
2013-07-30 15:13:53 +00:00
list.add(readPrimitive(json.get(i)));
}
return list;
}
else if(Map.class.isAssignableFrom(type)){
// TODO Add Map Support
Map map = (Map)type.newInstance();
for(int i=0; i<json.size(); i++){
map.put(
2013-07-30 15:13:53 +00:00
readPrimitive(json.get(i)),
readPrimitive(json.get(i)));
}
return map;
}
// Field is a new Object
else{
return readObject(json);
}
}
2013-07-30 15:13:53 +00:00
/**
* Unknown type, this method will try to guess
*/
protected static Object readPrimitive(DataNode json) throws UnsupportedDataTypeException{
throw new UnsupportedDataTypeException("Complex datatype like Lists and Maps not supported");
}
protected static Object readPrimitive(Class<?> type, DataNode json){
2013-07-30 15:13:53 +00:00
if (type == int.class ||
type == Integer.class) return json.getInt();
else if(type == long.class ||
type == Long.class) return json.getLong();
else if(type == double.class ||
type == Double.class) return json.getDouble();
else if(type == boolean.class ||
type == Boolean.class) return json.getBoolean();
else if(type == String.class) return json.getString();
return null;
}
2013-06-06 22:36:00 +00:00
public void readFully(byte[] b) throws IOException {
// TODO Auto-generated method stub
}
public void readFully(byte[] b, int off, int len) throws IOException {
// TODO Auto-generated method stub
}
public int skipBytes(int n) throws IOException {
// TODO Auto-generated method stub
return 0;
}
public boolean readBoolean() throws IOException {
// TODO Auto-generated method stub
return false;
}
public byte readByte() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public int readUnsignedByte() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public short readShort() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public int readUnsignedShort() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public char readChar() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public int readInt() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public long readLong() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public float readFloat() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public double readDouble() throws IOException {
// TODO Auto-generated method stub
return 0;
}
public String readLine() throws IOException {
// TODO Auto-generated method stub
return null;
}
public String readUTF() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return 0;
}
}