Many bugfixes and improvements

This commit is contained in:
Ziver Koc 2011-06-24 23:20:59 +00:00
parent c3e3bbf787
commit 363e0c6cfc
52 changed files with 2021 additions and 982 deletions

View file

@ -1,268 +0,0 @@
package zutil.parser.json;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This is an node in an JSON tree,
* it may contain a Map, list or value
*
* @author Ziver
*/
public class JSONNode implements Iterable<JSONNode>{
public enum JSONType{
Map, List, String, Number, Boolean
}
private Map<String,JSONNode> map = null;
private List<JSONNode> list = null;
private String value = null;
private JSONType type;
/**
* Creates an instance with an Boolean value
*/
public JSONNode(boolean value){
this.type = JSONType.Boolean;
this.value = ""+value;
}
/**
* Creates an instance with an int value
*/
public JSONNode(int value){
this.type = JSONType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an double value
*/
public JSONNode(double value){
this.type = JSONType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an long value
*/
public JSONNode(long value){
this.type = JSONType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an String value
*/
public JSONNode(String value){
this.type = JSONType.String;
this.value = value;
}
/**
* Creates an instance with a specific type
*/
public JSONNode(JSONType type){
this.type = type;
switch(type){
case Map:
map = new HashMap<String,JSONNode>(); break;
case List:
list = new LinkedList<JSONNode>(); break;
}
}
/**
* @param index is the index of the List or Map
* @return an JSONNode that contains the next level of the List or Map
*/
public JSONNode get(int index){
if(map != null)
return map.get(""+index);
else if(list != null)
return list.get(index);
return null;
}
/**
* @param index is the key in the Map
* @return an JSONNode that contains the next level of the Map
*/
public JSONNode get(String index){
if(map != null)
return map.get(index);
return null;
}
/**
* @return a iterator for the Map or List or null if the node contains a value
*/
public Iterator<JSONNode> iterator(){
if(map != null)
return map.values().iterator();
else if(list != null)
return list.iterator();
return null;
}
/**
* @return a iterator for the keys in the Map or null if the node contains a value or List
*/
public Iterator<String> keyIterator(){
if(map != null)
return map.keySet().iterator();
return null;
}
/**
* @return the size of the Map or List or -1 if it is a value
*/
public int size(){
if(map != null)
return map.size();
else if(list != null)
return list.size();
return -1;
}
/**
* Adds a node to the List
*/
public void add(JSONNode node){
list.add(node);
}
public void add(boolean value){
list.add(new JSONNode( value ));
}
public void add(int value){
list.add(new JSONNode( value ));
}
public void add(double value){
list.add(new JSONNode( value ));
}
public void add(long value){
list.add(new JSONNode( value ));
}
public void add(String value){
list.add(new JSONNode( value ));
}
/**
* Adds a node to the Map
*/
public void add(String key, JSONNode node){
map.put(key, node);
}
public void add(String key, boolean value){
map.put(key, new JSONNode(value));
}
public void add(String key, int value){
map.put(key, new JSONNode(value));
}
public void add(String key, double value){
map.put(key, new JSONNode(value));
}
public void add(String key, long value){
map.put(key, new JSONNode(value));
}
public void add(String key, String value){
map.put(key, new JSONNode(value));
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(int value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
this.value = ""+value;
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(double value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
this.value = ""+value;
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(boolean value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Boolean;
this.value = ""+value;
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(long value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
this.value = ""+value;
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(String value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.String;
this.value = value;
}
/**
* @return if this node contains an Map
*/
public boolean isMap(){
return type == JSONType.Map;
}
/**
* @return if this node contains an List
*/
public boolean isList(){
return type == JSONType.List;
}
/**
* @return if this node contains an value
*/
public boolean isValue(){
return type != JSONType.Map && type != JSONType.List;
}
/**
* @return the type of the node
*/
public JSONType getType(){
return type;
}
/**
* @return the String value in this node, null if its a Map or List
*/
public String getString(){
return value;
}
/**
* @return the boolean value in this node
*/
public boolean getBoolean(){
return Boolean.parseBoolean(value);
}
/**
* @return the integer value in this node
*/
public int getInt(){
return Integer.parseInt(value);
}
/**
* @return the double value in this node
*/
public double getDouble(){
return Double.parseDouble(value);
}
public String toString(){
if( this.isMap() )
return map.toString();
else if( this.isList() )
return list.toString();
return value;
}
}

View file

@ -1,7 +1,8 @@
package zutil.parser.json;
import zutil.io.MultiPrintStream;
import zutil.parser.json.JSONNode.JSONType;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
/**
* This is a JSON parser class
@ -28,7 +29,7 @@ public class JSONParser{
" { \"type\": \"fax\", \"number\": \"646 555-4567\" }"+
" ]"+
"}");
JSONNode node = parser.read();
DataNode node = parser.read();
MultiPrintStream.out.dump( node );
JSONWriter writer = new JSONWriter( System.out );
writer.write(node);
@ -48,7 +49,7 @@ public class JSONParser{
*
* @return a JSONNode object that is the root of the JSON
*/
public JSONNode read(){
public DataNode read(){
return parse(new StringBuffer(json));
}
@ -67,10 +68,10 @@ public class JSONParser{
* @param json
* @return
*/
protected static JSONNode parse(StringBuffer json){
JSONNode root = null;
JSONNode key = null;
JSONNode node = null;
protected static DataNode parse(StringBuffer json){
DataNode root = null;
DataNode key = null;
DataNode node = null;
int next_index;
while(Character.isWhitespace( json.charAt(0) ) ||
@ -84,26 +85,26 @@ public class JSONParser{
case '}':
return null;
case '{':
root = new JSONNode(JSONType.Map);
root = new DataNode(DataType.Map);
while((key = parse( json )) != null && (node = parse( json )) != null){
root.add( key.toString(), node );
root.set( key.toString(), node );
}
break;
case '[':
root = new JSONNode(JSONType.List);
root = new DataNode(DataType.List);
while((node = parse( json )) != null){
root.add( node );
}
break;
// Parse String
case '\"':
root = new JSONNode(JSONType.String);
root = new DataNode(DataType.String);
next_index = json.indexOf("\"");
root.set( json.substring(0, next_index) );
json.delete(0, next_index+1);
break;
default:
root = new JSONNode(JSONType.Number);
root = new DataNode(DataType.Number);
for(next_index=0; next_index<json.length() ;++next_index)
if( json.charAt(next_index)==',' ||
json.charAt(next_index)==']' ||

View file

@ -5,7 +5,8 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Iterator;
import zutil.parser.json.JSONNode.JSONType;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
/**
* Writes An JSONNode to an String or stream
@ -47,7 +48,7 @@ public class JSONWriter{
*
* @param root is the root node
*/
public void write(JSONNode root){
public void write(DataNode root){
boolean first = true;
switch(root.getType()){
// Write Map
@ -74,7 +75,7 @@ public class JSONWriter{
// Write an list
case List:
out.print('[');
for(JSONNode node : root){
for(DataNode node : root){
if(!first)
out.print(", ");
write( node );
@ -83,7 +84,7 @@ public class JSONWriter{
out.print(']');
break;
default:
if(root.getString() != null && root.getType() == JSONType.String){
if(root.getString() != null && root.getType() == DataType.String){
out.print('\"');
out.print(root.toString());
out.print('\"');