Improved JSON Node and Outputstream
This commit is contained in:
parent
7ccf26dc37
commit
86d463be88
5 changed files with 108 additions and 29 deletions
|
|
@ -23,6 +23,7 @@
|
|||
package zutil.parser.json;
|
||||
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNode.DataType;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Array;
|
||||
|
|
@ -32,6 +33,8 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.activation.UnsupportedDataTypeException;
|
||||
|
||||
public class JSONObjectOutputStream extends OutputStream implements ObjectOutput, Closeable{
|
||||
private boolean generateMetaData;
|
||||
|
||||
|
|
@ -96,13 +99,13 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
}
|
||||
}
|
||||
|
||||
protected DataNode getDataNode(Object obj) throws IllegalAccessException {
|
||||
protected DataNode getDataNode(Object obj) throws IOException, IllegalArgumentException, IllegalAccessException {
|
||||
//if(!(obj instanceof Serializable))
|
||||
// throw new UnSerializable
|
||||
|
||||
DataNode root = new DataNode(DataNode.DataType.Map);
|
||||
// Generate meta data
|
||||
if(generateMetaData){
|
||||
root.set("@class", obj.getClass().getName());
|
||||
// Cache
|
||||
if(objectCache.containsKey(obj)){ // Hit
|
||||
root.set("@object_id", objectCache.get(obj));
|
||||
|
|
@ -112,6 +115,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
objectCache.put(obj, objectCache.size()+1);
|
||||
root.set("@object_id", objectCache.size());
|
||||
}
|
||||
root.set("@class", obj.getClass().getName());
|
||||
}
|
||||
// Add all the fields to the DataNode
|
||||
for(Field field : obj.getClass().getDeclaredFields()){
|
||||
|
|
@ -121,7 +125,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
// Add basic type (int, float...)
|
||||
if(field.getType().isPrimitive() ||
|
||||
String.class.isAssignableFrom(field.getType())){
|
||||
root.set(field.getName(), field.get(obj).toString());
|
||||
root.set(field.getName(), getPrimitiveDataNode(field, obj));
|
||||
}
|
||||
// Add an array
|
||||
else if(field.getType().isArray()){
|
||||
|
|
@ -146,7 +150,33 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
private DataNode getPrimitiveDataNode(Field field, Object obj) throws UnsupportedDataTypeException, IllegalArgumentException, IllegalAccessException {
|
||||
Class<?> type = field.getType();
|
||||
DataNode node = null;
|
||||
if (type == int.class ||
|
||||
type == Integer.class ||
|
||||
type == long.class ||
|
||||
type == Long.class ||
|
||||
type == double.class ||
|
||||
type == Double.class)
|
||||
node = new DataNode(DataType.Number);
|
||||
|
||||
else if(type == boolean.class ||
|
||||
type == Boolean.class)
|
||||
node = new DataNode(DataType.Boolean);
|
||||
|
||||
else if(type == String.class ||
|
||||
type == char.class ||
|
||||
type == Character.class)
|
||||
node = new DataNode(DataType.String);
|
||||
else
|
||||
throw new UnsupportedDataTypeException("Unsupported primitive data type: "+type.getName());
|
||||
|
||||
node.set(field.get(obj).toString());
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
// TODO:
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@
|
|||
|
||||
package zutil.parser.json;
|
||||
|
||||
import zutil.io.StringInputStream;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNode.DataType;
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* This is a JSON parser class
|
||||
|
|
@ -35,6 +35,9 @@ import java.io.*;
|
|||
* @author Ziver
|
||||
*/
|
||||
public class JSONParser{
|
||||
public static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9.]++$");
|
||||
public static final Pattern BOOLEAN_PATTERN = Pattern.compile("^(true|false)$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private Reader in;
|
||||
|
||||
public JSONParser(Reader in){
|
||||
|
|
@ -112,19 +115,27 @@ public class JSONParser{
|
|||
str.append(c);
|
||||
root.set(str.toString());
|
||||
break;
|
||||
// Parse Number
|
||||
// Parse unknown type
|
||||
default:
|
||||
root = new DataNode(DataType.Number);
|
||||
StringBuilder num = new StringBuilder().append(c);
|
||||
StringBuilder tmp = new StringBuilder().append(c);
|
||||
while((c=(char)in.read()) != (char)-1 && !Character.isWhitespace(c) &&
|
||||
c != ',' && c != '='){
|
||||
if(c == ']' || c == '}'){
|
||||
end.i = 1;
|
||||
break;
|
||||
}
|
||||
num.append(c);
|
||||
tmp.append(c);
|
||||
}
|
||||
root.set(num.toString());
|
||||
// Check what type of type the data is
|
||||
String data = tmp.toString();
|
||||
System.out.println("\""+data+"\"");
|
||||
if( BOOLEAN_PATTERN.matcher(data).matches() )
|
||||
root = new DataNode(DataType.Boolean);
|
||||
else if( NUMBER_PATTERN.matcher(data).matches() )
|
||||
root = new DataNode(DataType.Number);
|
||||
else
|
||||
root = new DataNode(DataType.String);
|
||||
root.set(data);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue