Some progress on the JSONObjectInputStream
This commit is contained in:
parent
1a7f0fd1d9
commit
cdf1f13fb0
5 changed files with 137 additions and 38 deletions
|
|
@ -47,16 +47,23 @@ public class Base64Decoder {
|
|||
output = new DynamicByteArrayStream();
|
||||
}
|
||||
|
||||
|
||||
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 String decode( String data ){
|
||||
public static byte[] decodeToByte( String data ){
|
||||
Base64Decoder base64 = new Base64Decoder();
|
||||
base64.write( data );
|
||||
return base64.toString();
|
||||
return base64.getByte();
|
||||
}
|
||||
|
||||
public void write( String data ){
|
||||
|
|
|
|||
|
|
@ -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<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
|
||||
List list = (List)type.newInstance();
|
||||
for(int i=0; i<json.size(); i++){
|
||||
list.add(readValue(null, 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(
|
||||
readValue(null, json.get(i)),
|
||||
readValue(null, json.get(i)));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
// Field is a new Object
|
||||
else{
|
||||
return readObject(json);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Object readPrimitive(Class<?> 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
|
||||
|
|
|
|||
|
|
@ -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<Object,Integer> objectCache;
|
||||
private JSONWriter out;
|
||||
|
||||
public JSONObjectOutputStream(OutputStream out) {
|
||||
this.generateMetaDeta = true;
|
||||
this.generateMetaData = true;
|
||||
this.objectCache = new HashMap<Object, Integer>();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue