diff --git a/src/zutil/parser/Base64Decoder.java b/src/zutil/parser/Base64Decoder.java index 762a8b7..dbe9c73 100644 --- a/src/zutil/parser/Base64Decoder.java +++ b/src/zutil/parser/Base64Decoder.java @@ -46,18 +46,25 @@ public class Base64Decoder { public Base64Decoder(){ output = new DynamicByteArrayStream(); } - - public static String decodeToHex( String data ){ - Base64Decoder base64 = new Base64Decoder(); - base64.write( data ); - return Converter.toHexString( base64.getByte() ); - } + public static String decode( String data ){ Base64Decoder base64 = new Base64Decoder(); base64.write( data ); return base64.toString(); } + + public static String decodeToHex( String data ){ + Base64Decoder base64 = new Base64Decoder(); + base64.write( data ); + return Converter.toHexString( base64.getByte() ); + } + + public static byte[] decodeToByte( String data ){ + Base64Decoder base64 = new Base64Decoder(); + base64.write( data ); + return base64.getByte(); + } public void write( String data ){ byte[] buffer = new byte[ (data.length()*6/8) + 1 ]; diff --git a/src/zutil/parser/json/JSONObjectInputStream.java b/src/zutil/parser/json/JSONObjectInputStream.java index 0a117d0..8e07c66 100644 --- a/src/zutil/parser/json/JSONObjectInputStream.java +++ b/src/zutil/parser/json/JSONObjectInputStream.java @@ -22,17 +22,112 @@ package zutil.parser.json; -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInput; +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; +import java.util.List; +import java.util.Map; public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{ + private JSONParser parser; - public JSONObjectInputStream(InputStream bout) { - // TODO Auto-generated constructor stub + public JSONObjectInputStream(Reader in) { + this.parser = new JSONParser(in); } + public Object readObject() throws ClassNotFoundException, IOException { + try{ + DataNode root = parser.read(); + if(root != null){ + readObject(root); + } + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + protected static Object readObject(DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException { + 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( + field.getDeclaringClass(), + json.get(field.getName()))); + } + } + return obj; + } + + protected static Object readValue(Class type, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException { + // Field type is a primitive? + if(type.isPrimitive()){ + 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 type, DataNode json){ + //if (type == Short.class) return json.getShort(); + if (type == Integer.class) return json.getInt(); + else if(type == Long.class) return json.getLong(); + + //else if(type == Float.class) field.setFloat(obj, json.getFloat()); + else if(type == Double.class) return json.getDouble(); + + else if(type == Boolean.class) return json.getBoolean(); + //else if(type == Character.class) return json.getChar(); + else if(type == String.class) return json.getString(); + //else if(type == Byte.class) return Base64Decoder.decodeToByte(json.getString()); + return null; + } + + public void readFully(byte[] b) throws IOException { // TODO Auto-generated method stub @@ -108,11 +203,6 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C return null; } - public Object readObject() throws ClassNotFoundException, IOException { - // TODO Auto-generated method stub - return null; - } - @Override public int read() throws IOException { // TODO Auto-generated method stub diff --git a/src/zutil/parser/json/JSONObjectOutputStream.java b/src/zutil/parser/json/JSONObjectOutputStream.java index 23f21d5..e72bd18 100644 --- a/src/zutil/parser/json/JSONObjectOutputStream.java +++ b/src/zutil/parser/json/JSONObjectOutputStream.java @@ -22,7 +22,6 @@ package zutil.parser.json; -import zutil.log.LogUtil; import zutil.parser.DataNode; import java.io.*; @@ -30,17 +29,17 @@ import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.List; +import java.util.Map; public class JSONObjectOutputStream extends OutputStream implements ObjectOutput, Closeable{ - private boolean generateMetaDeta; + private boolean generateMetaData; private HashMap objectCache; private JSONWriter out; public JSONObjectOutputStream(OutputStream out) { - this.generateMetaDeta = true; + this.generateMetaData = true; this.objectCache = new HashMap(); this.out = new JSONWriter(out); } @@ -102,7 +101,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput // throw new UnSerializable DataNode root = new DataNode(DataNode.DataType.Map); // Generate meta data - if(generateMetaDeta){ + if(generateMetaData){ root.set("@class", obj.getClass().getName()); // Cache if(objectCache.containsKey(obj)){ // Hit @@ -119,8 +118,13 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput if((field.getModifiers() & Modifier.STATIC) == 0 && (field.getModifiers() & Modifier.TRANSIENT) == 0){ field.setAccessible(true); + // Add basic type (int, float...) + if(field.getType().isPrimitive() || + field.getType() == String.class){ + root.set(field.getName(), field.get(obj).toString()); + } // Add an array - if(field.getType().isArray()){ + else if(field.getType().isArray()){ DataNode arrayNode = new DataNode(DataNode.DataType.List); Object array = field.get(obj); for(int i=0; i< Array.getLength(array) ;i++){ @@ -128,10 +132,11 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput } root.set(field.getName(), arrayNode); } - // Add basic type (int, float...) - else if(field.getType().isPrimitive() || - field.getType() == String.class){ - root.set(field.getName(), field.get(obj).toString()); + else if(List.class.isAssignableFrom(field.getType())){ + // TODO Add List Support + } + else if(Map.class.isAssignableFrom(field.getType())){ + // TODO Add Map Support } else{ root.set(field.getName(), getDataNode(field.get(obj))); @@ -152,8 +157,8 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput * Should only be disabled if the input is not a JSONObjectInputStream. * All the meta-data tags will start with a '@' */ - public void generateMetaDeta(boolean generate){ - generateMetaDeta = generate; + public void generateMetaData(boolean generate){ + generateMetaData = generate; } /** diff --git a/src/zutil/parser/json/JSONParser.java b/src/zutil/parser/json/JSONParser.java index b03d5b8..d1b7f0b 100644 --- a/src/zutil/parser/json/JSONParser.java +++ b/src/zutil/parser/json/JSONParser.java @@ -35,7 +35,7 @@ import java.io.*; * @author Ziver */ public class JSONParser{ - protected Reader in; + private Reader in; public JSONParser(Reader in){ this.in = in; diff --git a/src/zutil/test/JSONSerializerTest.java b/src/zutil/test/JSONSerializerTest.java index 62df785..49c5220 100644 --- a/src/zutil/test/JSONSerializerTest.java +++ b/src/zutil/test/JSONSerializerTest.java @@ -24,12 +24,7 @@ package zutil.test; import static org.junit.Assert.assertEquals; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; +import java.io.*; import org.junit.Test; @@ -50,6 +45,7 @@ public class JSONSerializerTest{ out.flush(); out.close(); String data = bout.toString(); + System.out.println(data); assertEquals( "{\"str\": \"1234\", \"@class\": \"zutil.test.JSONSerializerTest$TestClass\", \"obj1\": {\"@class\": \"zutil.test.JSONSerializerTest$TestObj\", \"value\": \"42\", \"@object_id\": 2}, \"obj2\": {\"@class\": \"zutil.test.JSONSerializerTest$TestObj\", \"value\": \"42\", \"@object_id\": 3}, \"@object_id\": 1}", @@ -85,8 +81,9 @@ public class JSONSerializerTest{ out.flush(); out.close(); String data = bout.toString(); + System.out.println(data); - StringInputStream bin = new StringInputStream(data); + StringReader bin = new StringReader(data); JSONObjectInputStream in = new JSONObjectInputStream(bin); TestClass targetObj = (TestClass) in.readObject(); in.close();