RESOLVED - # 99: Change StringBuffer deleteCharAt() to integer
http://bugs.koc.se/view.php?id=99
This commit is contained in:
parent
c4cfa1debb
commit
49387c7f31
1 changed files with 22 additions and 58 deletions
|
|
@ -21,7 +21,6 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package zutil.parser.json;
|
package zutil.parser.json;
|
||||||
|
|
||||||
import zutil.io.MultiPrintStream;
|
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.DataNode.DataType;
|
import zutil.parser.DataNode.DataType;
|
||||||
|
|
||||||
|
|
@ -31,57 +30,23 @@ import zutil.parser.DataNode.DataType;
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public class JSONParser{
|
public class JSONParser{
|
||||||
private String json;
|
/**
|
||||||
|
* A dumb class for persisting an index
|
||||||
public static void main(String[] args){
|
*/
|
||||||
JSONParser parser = new JSONParser("" +
|
protected class Index{
|
||||||
"{"+
|
protected int i = 0;
|
||||||
" \"firstName\": \"John\","+
|
|
||||||
" \"lastName\": \"Smith\","+
|
|
||||||
" \"age\": 25,"+
|
|
||||||
" \"address\": {"+
|
|
||||||
" \"streetAddress\": \"21 2nd Street\","+
|
|
||||||
" \"city\": \"New York\","+
|
|
||||||
" \"state\": \"NY\","+
|
|
||||||
" \"postalCode\": \"10021\""+
|
|
||||||
" },"+
|
|
||||||
" \"phoneNumber\": ["+
|
|
||||||
" { \"type\": \"home\", \"number\": \"212 555-1234\" },"+
|
|
||||||
" { \"type\": \"fax\", \"number\": \"646 555-4567\" }"+
|
|
||||||
" ]"+
|
|
||||||
"}");
|
|
||||||
DataNode node = parser.read();
|
|
||||||
MultiPrintStream.out.dump( node );
|
|
||||||
JSONWriter writer = new JSONWriter( System.out );
|
|
||||||
writer.write(node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of the parser
|
* Starts parsing
|
||||||
*
|
*
|
||||||
* @param json is the JSON String to parse
|
* @param json is the JSON String to parse
|
||||||
*/
|
|
||||||
public JSONParser(String json){
|
|
||||||
this.json = json;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts parsing
|
|
||||||
*
|
|
||||||
* @return a JSONNode object that is the root of the JSON
|
* @return a JSONNode object that is the root of the JSON
|
||||||
*/
|
*/
|
||||||
public DataNode read(){
|
public DataNode read(String json){
|
||||||
return parse(new StringBuffer(json));
|
return parse(new Index(), new StringBuffer(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts parsing
|
|
||||||
*
|
|
||||||
* @return a JSONNode object that is the root of the JSON
|
|
||||||
*/
|
|
||||||
/*public static JSONNode read( String json ){
|
|
||||||
return parse(new StringBuffer(json));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the real recursive parsing method
|
* This is the real recursive parsing method
|
||||||
|
|
@ -89,17 +54,16 @@ public class JSONParser{
|
||||||
* @param json
|
* @param json
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected static DataNode parse(StringBuffer json){
|
protected static DataNode parse(Index index, StringBuffer json){
|
||||||
DataNode root = null;
|
DataNode root = null;
|
||||||
DataNode key = null;
|
DataNode key = null;
|
||||||
DataNode node = null;
|
DataNode node = null;
|
||||||
int next_index;
|
int next_index;
|
||||||
|
|
||||||
while(Character.isWhitespace( json.charAt(0) ) ||
|
while(Character.isWhitespace( json.charAt(index.i) ) ||
|
||||||
json.charAt(0) == ',' || json.charAt(0) == ':')
|
json.charAt(index.i) == ',' || json.charAt(index.i) == ':')
|
||||||
json.deleteCharAt(0);
|
index.i++;
|
||||||
char c = json.charAt(0);
|
char c = json.charAt(index.i++);
|
||||||
json.deleteCharAt(0);
|
|
||||||
|
|
||||||
switch( c ){
|
switch( c ){
|
||||||
case ']':
|
case ']':
|
||||||
|
|
@ -107,33 +71,33 @@ public class JSONParser{
|
||||||
return null;
|
return null;
|
||||||
case '{':
|
case '{':
|
||||||
root = new DataNode(DataType.Map);
|
root = new DataNode(DataType.Map);
|
||||||
while((key = parse( json )) != null && (node = parse( json )) != null){
|
while((key = parse( index, json )) != null && (node = parse( index, json )) != null){
|
||||||
root.set( key.toString(), node );
|
root.set( key.toString(), node );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
root = new DataNode(DataType.List);
|
root = new DataNode(DataType.List);
|
||||||
while((node = parse( json )) != null){
|
while((node = parse( index, json )) != null){
|
||||||
root.add( node );
|
root.add( node );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// Parse String
|
// Parse String
|
||||||
case '\"':
|
case '\"':
|
||||||
root = new DataNode(DataType.String);
|
root = new DataNode(DataType.String);
|
||||||
next_index = json.indexOf("\"");
|
next_index = json.indexOf( "\"", index.i);
|
||||||
root.set( json.substring(0, next_index) );
|
root.set( json.substring(index.i, next_index) );
|
||||||
json.delete(0, next_index+1);
|
index.i = next_index+1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
root = new DataNode(DataType.Number);
|
root = new DataNode(DataType.Number);
|
||||||
for(next_index=0; next_index<json.length() ;++next_index)
|
for(next_index=index.i; next_index<json.length() ;++next_index)
|
||||||
if( json.charAt(next_index)==',' ||
|
if( json.charAt(next_index)==',' ||
|
||||||
json.charAt(next_index)==']' ||
|
json.charAt(next_index)==']' ||
|
||||||
json.charAt(next_index)=='}'){
|
json.charAt(next_index)=='}'){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
root.set( c+json.substring(0, next_index) );
|
root.set( c+json.substring(index.i, next_index) );
|
||||||
json.delete(0, next_index);
|
index.i = next_index;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue