Updated JSONParser to read from a stream

This commit is contained in:
Ziver Koc 2013-06-09 00:00:00 +00:00
parent 22adf3b7f2
commit 1a7f0fd1d9
3 changed files with 102 additions and 84 deletions

View file

@ -61,9 +61,12 @@ public class StringInputStream extends InputStream{
* Reads the next byte of data from the input stream. * Reads the next byte of data from the input stream.
*/ */
public int read(){ public int read(){
int ret = Character.getNumericValue( buffer.charAt( 0 )); if(buffer.length() > 0){
buffer.deleteCharAt( 0 ); int ret = Character.getNumericValue( buffer.charAt( 0 ));
return ret; buffer.deleteCharAt( 0 );
return ret;
}
return -1;
} }
/** /**

View file

@ -22,11 +22,12 @@
package zutil.parser.json; package zutil.parser.json;
import zutil.io.StringInputStream;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType; import zutil.parser.DataNode.DataType;
import zutil.struct.MutableInt; import zutil.struct.MutableInt;
import java.io.BufferedReader; import java.io.*;
/** /**
* This is a JSON parser class * This is a JSON parser class
@ -34,9 +35,9 @@ import java.io.BufferedReader;
* @author Ziver * @author Ziver
*/ */
public class JSONParser{ public class JSONParser{
protected Readable in; protected Reader in;
public JSONParser(Readable in){ public JSONParser(Reader in){
this.in = in; this.in = in;
} }
@ -46,8 +47,8 @@ public class JSONParser{
* *
* @return a DataNode object representing the input JSON * @return a DataNode object representing the input JSON
*/ */
public DataNode read(){ public DataNode read() throws IOException {
return parse(in); return parse(in, new MutableInt());
} }
/** /**
@ -57,112 +58,76 @@ public class JSONParser{
* @return a DataNode object representing the JSON in the input String * @return a DataNode object representing the JSON in the input String
*/ */
public static DataNode read(String json){ public static DataNode read(String json){
return parse(new MutableInt(), new StringBuilder(json)); try{
return parse(new StringReader(json), new MutableInt());
}catch (IOException e){
e.printStackTrace();
}catch (NullPointerException e){}
return null;
} }
/** /**
* This is the real recursive parsing method * This is the real recursive parsing method
*/ */
protected static DataNode parse(Readable in){ protected static DataNode parse(Reader in, MutableInt end) throws IOException {
DataNode root = null; DataNode root = null;
DataNode key = null; DataNode key = null;
DataNode node = null; DataNode node = null;
int next_index; end.i = 0;
while(Character.isWhitespace( json.charAt(index.i) ) || char c = '_';
json.charAt(index.i) == ',' || json.charAt(index.i) == ':') while((c=(char)in.read()) < 0 || Character.isWhitespace(c) ||
index.i++; c == ',' || c == ':');
char c = json.charAt(index.i++);
switch( c ){ switch( c ){
// This is the end of an Map or List
case ']': case ']':
case '}': case '}':
case (char)-1:
end.i = 1;
return null; return null;
// Parse Map
case '{': case '{':
root = new DataNode(DataType.Map); root = new DataNode(DataType.Map);
while((key = parse( index, json )) != null && (node = parse( index, json )) != null){ while(end.i != 1 &&
(key = parse(in, end)) != null &&
(node = parse(in, end)) != null){
root.set( key.toString(), node ); root.set( key.toString(), node );
} }
end.i = 0;
break; break;
// Parse List
case '[': case '[':
root = new DataNode(DataType.List); root = new DataNode(DataType.List);
while((node = parse( index, json )) != null){ while(end.i != 1 && (node = parse(in, end)) != null){
root.add( node ); root.add( node );
} }
end.i = 0;
break; break;
// Parse String // Parse String
case '\"': case '\"':
root = new DataNode(DataType.String); root = new DataNode(DataType.String);
next_index = json.indexOf( "\"", index.i); StringBuilder str = new StringBuilder();
root.set( json.substring(index.i, next_index) ); while((c=(char)in.read()) != (char)-1 && c != '\"')
index.i = next_index+1; str.append(c);
root.set(str.toString());
break; break;
// Parse Number
default: default:
root = new DataNode(DataType.Number); root = new DataNode(DataType.Number);
for(next_index=index.i; next_index<json.length() ;++next_index) StringBuilder num = new StringBuilder().append(c);
if( json.charAt(next_index)==',' || while((c=(char)in.read()) != (char)-1 && !Character.isWhitespace(c) &&
json.charAt(next_index)==']' || c != ',' && c != '='){
json.charAt(next_index)=='}'){ if(c == ']' || c == '}'){
end.i = 1;
break; break;
} }
root.set( c+json.substring(index.i, next_index) ); num.append(c);
index.i = next_index; }
root.set(num.toString());
break; break;
} }
return root; return root;
} }
/**
* This is the real recursive parsing method
*/
protected static DataNode parse(MutableInt index, StringBuilder json){
DataNode root = null;
DataNode key = null;
DataNode node = null;
int next_index;
while(Character.isWhitespace( json.charAt(index.i) ) ||
json.charAt(index.i) == ',' || json.charAt(index.i) == ':')
index.i++;
char c = json.charAt(index.i++);
switch( c ){
case ']':
case '}':
return null;
case '{':
root = new DataNode(DataType.Map);
while((key = parse( index, json )) != null && (node = parse( index, json )) != null){
root.set( key.toString(), node );
}
break;
case '[':
root = new DataNode(DataType.List);
while((node = parse( index, json )) != null){
root.add( node );
}
break;
// Parse String
case '\"':
root = new DataNode(DataType.String);
next_index = json.indexOf( "\"", index.i);
root.set( json.substring(index.i, next_index) );
index.i = next_index+1;
break;
default:
root = new DataNode(DataType.Number);
for(next_index=index.i; next_index<json.length() ;++next_index)
if( json.charAt(next_index)==',' ||
json.charAt(next_index)==']' ||
json.charAt(next_index)=='}'){
break;
}
root.set( c+json.substring(index.i, next_index) );
index.i = next_index;
break;
}
return root;
}
} }

View file

@ -23,6 +23,7 @@
package zutil.test; package zutil.test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test; import org.junit.Test;
@ -33,20 +34,69 @@ import zutil.parser.json.JSONParser;
public class JSONTest{ public class JSONTest{
@Test
public void nullString(){
DataNode data = JSONParser.read(null);
assertNull(data);
}
@Test
public void emptyString(){
DataNode data = JSONParser.read("");
assertNull(data);
}
@Test
public void emptyMap(){
DataNode data = JSONParser.read("{}");
assert(data.isMap());
assertEquals( 0, data.size());
}
@Test
public void emptyList(){
DataNode data = JSONParser.read("[]");
assert(data.isList());
assertEquals( 0, data.size());
}
@Test
public void number(){
DataNode data = JSONParser.read("1234");
assert(data.isValue());
assertEquals( 1234, data.getInt());
}
@Test
public void toManyCommasInList(){
DataNode data = JSONParser.read("[1,2,3,]");
assert(data.isList());
assertEquals( 1, data.get(0).getInt());
assertEquals( 2, data.get(1).getInt());
assertEquals( 3, data.get(2).getInt());
}
@Test
public void toManyCommasInMap(){
DataNode data = JSONParser.read("{1=1,2=2,3=3,}");
assert(data.isMap());
assertEquals( 1, data.get("1").getInt());
assertEquals( 2, data.get("2").getInt());
assertEquals( 3, data.get("3").getInt());
}
@Test @Test
public void JSONParser() { public void complexMap() {
JSONParser parser = new JSONParser();
String json = "{" + String json = "{" +
"\"test1\": 1234," + "\"test1\": 1234," +
"\"TEST1\": 5678," + "\"TEST1\": 5678," +
"\"test3\": 1234.99," + "\"test3\": 1234.99," +
"\"test4\": \"91011\"," + "\"test4\": \"91011\"," +
"\"test5\": [12,13,14,15]," + "\"test5\": [12,13,14,15]," +
"\"test6\": [\"a\",\"b\",\"c\",\"d\"]," + "\"test6\": [\"a\",\"b\",\"c\",\"d\"]" +
"}"; "}";
DataNode data = parser.read(json); DataNode data = JSONParser.read(json);
assert( data.isMap() ); assert( data.isMap() );
assert( 1234 == data.get("test1").getInt() ); assert( 1234 == data.get("test1").getInt() );
assert( 5678 == data.get("TEST1").getInt() ); assert( 5678 == data.get("TEST1").getInt() );