Some progress on the JSONObjectInputStream
This commit is contained in:
parent
1a7f0fd1d9
commit
cdf1f13fb0
5 changed files with 137 additions and 38 deletions
|
|
@ -47,11 +47,6 @@ public class Base64Decoder {
|
||||||
output = new DynamicByteArrayStream();
|
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 ){
|
public static String decode( String data ){
|
||||||
Base64Decoder base64 = new Base64Decoder();
|
Base64Decoder base64 = new Base64Decoder();
|
||||||
|
|
@ -59,6 +54,18 @@ public class Base64Decoder {
|
||||||
return base64.toString();
|
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 ){
|
public void write( String data ){
|
||||||
byte[] buffer = new byte[ (data.length()*6/8) + 1 ];
|
byte[] buffer = new byte[ (data.length()*6/8) + 1 ];
|
||||||
int buffi = 0;
|
int buffi = 0;
|
||||||
|
|
|
||||||
|
|
@ -22,17 +22,112 @@
|
||||||
|
|
||||||
package zutil.parser.json;
|
package zutil.parser.json;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import zutil.parser.Base64Decoder;
|
||||||
import java.io.IOException;
|
import zutil.parser.DataNode;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.ObjectInput;
|
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{
|
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
|
||||||
|
private JSONParser parser;
|
||||||
|
|
||||||
public JSONObjectInputStream(InputStream bout) {
|
public JSONObjectInputStream(Reader in) {
|
||||||
// TODO Auto-generated constructor stub
|
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 {
|
public void readFully(byte[] b) throws IOException {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
@ -108,11 +203,6 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object readObject() throws ClassNotFoundException, IOException {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@
|
||||||
|
|
||||||
package zutil.parser.json;
|
package zutil.parser.json;
|
||||||
|
|
||||||
import zutil.log.LogUtil;
|
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
@ -30,17 +29,17 @@ import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.logging.Level;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.Map;
|
||||||
|
|
||||||
public class JSONObjectOutputStream extends OutputStream implements ObjectOutput, Closeable{
|
public class JSONObjectOutputStream extends OutputStream implements ObjectOutput, Closeable{
|
||||||
private boolean generateMetaDeta;
|
private boolean generateMetaData;
|
||||||
|
|
||||||
private HashMap<Object,Integer> objectCache;
|
private HashMap<Object,Integer> objectCache;
|
||||||
private JSONWriter out;
|
private JSONWriter out;
|
||||||
|
|
||||||
public JSONObjectOutputStream(OutputStream out) {
|
public JSONObjectOutputStream(OutputStream out) {
|
||||||
this.generateMetaDeta = true;
|
this.generateMetaData = true;
|
||||||
this.objectCache = new HashMap<Object, Integer>();
|
this.objectCache = new HashMap<Object, Integer>();
|
||||||
this.out = new JSONWriter(out);
|
this.out = new JSONWriter(out);
|
||||||
}
|
}
|
||||||
|
|
@ -102,7 +101,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
||||||
// throw new UnSerializable
|
// throw new UnSerializable
|
||||||
DataNode root = new DataNode(DataNode.DataType.Map);
|
DataNode root = new DataNode(DataNode.DataType.Map);
|
||||||
// Generate meta data
|
// Generate meta data
|
||||||
if(generateMetaDeta){
|
if(generateMetaData){
|
||||||
root.set("@class", obj.getClass().getName());
|
root.set("@class", obj.getClass().getName());
|
||||||
// Cache
|
// Cache
|
||||||
if(objectCache.containsKey(obj)){ // Hit
|
if(objectCache.containsKey(obj)){ // Hit
|
||||||
|
|
@ -119,8 +118,13 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
||||||
if((field.getModifiers() & Modifier.STATIC) == 0 &&
|
if((field.getModifiers() & Modifier.STATIC) == 0 &&
|
||||||
(field.getModifiers() & Modifier.TRANSIENT) == 0){
|
(field.getModifiers() & Modifier.TRANSIENT) == 0){
|
||||||
field.setAccessible(true);
|
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
|
// Add an array
|
||||||
if(field.getType().isArray()){
|
else if(field.getType().isArray()){
|
||||||
DataNode arrayNode = new DataNode(DataNode.DataType.List);
|
DataNode arrayNode = new DataNode(DataNode.DataType.List);
|
||||||
Object array = field.get(obj);
|
Object array = field.get(obj);
|
||||||
for(int i=0; i< Array.getLength(array) ;i++){
|
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);
|
root.set(field.getName(), arrayNode);
|
||||||
}
|
}
|
||||||
// Add basic type (int, float...)
|
else if(List.class.isAssignableFrom(field.getType())){
|
||||||
else if(field.getType().isPrimitive() ||
|
// TODO Add List Support
|
||||||
field.getType() == String.class){
|
}
|
||||||
root.set(field.getName(), field.get(obj).toString());
|
else if(Map.class.isAssignableFrom(field.getType())){
|
||||||
|
// TODO Add Map Support
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
root.set(field.getName(), getDataNode(field.get(obj)));
|
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.
|
* Should only be disabled if the input is not a JSONObjectInputStream.
|
||||||
* All the meta-data tags will start with a '@'
|
* All the meta-data tags will start with a '@'
|
||||||
*/
|
*/
|
||||||
public void generateMetaDeta(boolean generate){
|
public void generateMetaData(boolean generate){
|
||||||
generateMetaDeta = generate;
|
generateMetaData = generate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ import java.io.*;
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public class JSONParser{
|
public class JSONParser{
|
||||||
protected Reader in;
|
private Reader in;
|
||||||
|
|
||||||
public JSONParser(Reader in){
|
public JSONParser(Reader in){
|
||||||
this.in = in;
|
this.in = in;
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,7 @@ package zutil.test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.*;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -50,6 +45,7 @@ public class JSONSerializerTest{
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
String data = bout.toString();
|
String data = bout.toString();
|
||||||
|
System.out.println(data);
|
||||||
|
|
||||||
assertEquals(
|
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}",
|
"{\"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.flush();
|
||||||
out.close();
|
out.close();
|
||||||
String data = bout.toString();
|
String data = bout.toString();
|
||||||
|
System.out.println(data);
|
||||||
|
|
||||||
StringInputStream bin = new StringInputStream(data);
|
StringReader bin = new StringReader(data);
|
||||||
JSONObjectInputStream in = new JSONObjectInputStream(bin);
|
JSONObjectInputStream in = new JSONObjectInputStream(bin);
|
||||||
TestClass targetObj = (TestClass) in.readObject();
|
TestClass targetObj = (TestClass) in.readObject();
|
||||||
in.close();
|
in.close();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue