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,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)==']' ||