Fixed some warnings and did some speed improvments to json parsers
This commit is contained in:
parent
b9a662e4b3
commit
7aa5bc2a0e
6 changed files with 106 additions and 558 deletions
|
|
@ -22,7 +22,6 @@
|
|||
|
||||
package zutil.parser.json;
|
||||
|
||||
import sun.misc.ClassLoaderUtil;
|
||||
import zutil.ClassUtil;
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNode.DataType;
|
||||
|
|
@ -109,7 +108,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
}
|
||||
|
||||
protected DataNode getDataNode(Object obj) throws IOException, IllegalArgumentException, IllegalAccessException {
|
||||
DataNode root = null;
|
||||
DataNode root;
|
||||
|
||||
// Check if the object is a primitive
|
||||
if(ClassUtil.isPrimitive(obj.getClass()) ||
|
||||
|
|
@ -168,7 +167,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
}
|
||||
|
||||
private DataNode getPrimitiveDataNode(Class<?> type, Object value) throws UnsupportedDataTypeException, IllegalArgumentException, IllegalAccessException {
|
||||
DataNode node = null;
|
||||
DataNode node;
|
||||
if (type == int.class ||
|
||||
type == Integer.class ||
|
||||
type == long.class ||
|
||||
|
|
@ -202,4 +201,8 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
|
|||
generateMetaData = generate;
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
super.flush();
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,97 +33,91 @@ import zutil.parser.DataNode.DataType;
|
|||
|
||||
/**
|
||||
* Writes An JSONNode to an String or stream
|
||||
*
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class JSONWriter{
|
||||
private PrintWriter out;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(OutputStream out){
|
||||
this( new PrintWriter(out) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(PrintStream out){
|
||||
this( new PrintWriter(out) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(PrintWriter out){
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified node to the stream
|
||||
*
|
||||
* @param root is the root node
|
||||
*/
|
||||
public void write(DataNode root){
|
||||
boolean first = true;
|
||||
switch(root.getType()){
|
||||
// Write Map
|
||||
case Map:
|
||||
out.print('{');
|
||||
Iterator<String> it = root.keyIterator();
|
||||
while(it.hasNext()){
|
||||
if(!first)
|
||||
out.print(", ");
|
||||
String key = it.next();
|
||||
try{
|
||||
out.print( Integer.parseInt(key) );
|
||||
}catch(Exception e){
|
||||
out.print('\"');
|
||||
out.print(key);
|
||||
out.print('\"');
|
||||
}
|
||||
out.print(": ");
|
||||
write( root.get(key) );
|
||||
first = false;
|
||||
}
|
||||
out.print('}');
|
||||
break;
|
||||
// Write an list
|
||||
case List:
|
||||
out.print('[');
|
||||
for(DataNode node : root){
|
||||
if(!first)
|
||||
out.print(", ");
|
||||
write( node );
|
||||
first = false;
|
||||
}
|
||||
out.print(']');
|
||||
break;
|
||||
default:
|
||||
if(root.getString() != null && root.getType() == DataType.String){
|
||||
out.print('\"');
|
||||
out.print(root.toString());
|
||||
out.print('\"');
|
||||
} else
|
||||
out.print(root.toString());
|
||||
break;
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the internal stream
|
||||
*/
|
||||
public void close(){
|
||||
out.close();
|
||||
}
|
||||
private PrintWriter out;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(OutputStream out){
|
||||
this( new PrintWriter(out) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(PrintStream out){
|
||||
this( new PrintWriter(out) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the writer
|
||||
*
|
||||
* @param out the OutputStream that the Nodes will be sent to
|
||||
*/
|
||||
public JSONWriter(PrintWriter out){
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the specified node to the stream
|
||||
*
|
||||
* @param root is the root node
|
||||
*/
|
||||
public void write(DataNode root){
|
||||
boolean first = true;
|
||||
switch(root.getType()){
|
||||
// Write Map
|
||||
case Map:
|
||||
out.append('{');
|
||||
Iterator<String> it = root.keyIterator();
|
||||
while(it.hasNext()){
|
||||
if(!first)
|
||||
out.append(", ");
|
||||
String key = it.next();
|
||||
out.append('\"');
|
||||
out.append(key);
|
||||
out.append("\": ");
|
||||
write(root.get(key));
|
||||
first = false;
|
||||
}
|
||||
out.append('}');
|
||||
break;
|
||||
// Write List
|
||||
case List:
|
||||
out.append('[');
|
||||
for(DataNode node : root){
|
||||
if(!first)
|
||||
out.append(", ");
|
||||
write(node);
|
||||
first = false;
|
||||
}
|
||||
out.append(']');
|
||||
break;
|
||||
default:
|
||||
if(root.getString() != null && root.getType() == DataType.String){
|
||||
out.append('\"');
|
||||
out.append(root.toString());
|
||||
out.append('\"');
|
||||
} else
|
||||
out.append(root.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the internal stream
|
||||
*/
|
||||
public void close(){
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return JSON String that is generated from the input DataNode graph
|
||||
|
|
@ -135,4 +129,8 @@ public class JSONWriter{
|
|||
writer.close();
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public void flush(){
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue