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.
*/
public int read(){
int ret = Character.getNumericValue( buffer.charAt( 0 ));
buffer.deleteCharAt( 0 );
return ret;
if(buffer.length() > 0){
int ret = Character.getNumericValue( buffer.charAt( 0 ));
buffer.deleteCharAt( 0 );
return ret;
}
return -1;
}
/**

View file

@ -22,11 +22,12 @@
package zutil.parser.json;
import zutil.io.StringInputStream;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
import zutil.struct.MutableInt;
import java.io.BufferedReader;
import java.io.*;
/**
* This is a JSON parser class
@ -34,9 +35,9 @@ import java.io.BufferedReader;
* @author Ziver
*/
public class JSONParser{
protected Readable in;
protected Reader in;
public JSONParser(Readable in){
public JSONParser(Reader in){
this.in = in;
}
@ -46,8 +47,8 @@ public class JSONParser{
*
* @return a DataNode object representing the input JSON
*/
public DataNode read(){
return parse(in);
public DataNode read() throws IOException {
return parse(in, new MutableInt());
}
/**
@ -57,112 +58,76 @@ public class JSONParser{
* @return a DataNode object representing the JSON in the input String
*/
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
*/
protected static DataNode parse(Readable in){
protected static DataNode parse(Reader in, MutableInt end) throws IOException {
DataNode root = null;
DataNode key = null;
DataNode node = null;
int next_index;
end.i = 0;
while(Character.isWhitespace( json.charAt(index.i) ) ||
json.charAt(index.i) == ',' || json.charAt(index.i) == ':')
index.i++;
char c = json.charAt(index.i++);
char c = '_';
while((c=(char)in.read()) < 0 || Character.isWhitespace(c) ||
c == ',' || c == ':');
switch( c ){
// This is the end of an Map or List
case ']':
case '}':
case (char)-1:
end.i = 1;
return null;
// Parse Map
case '{':
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 );
}
end.i = 0;
break;
// Parse List
case '[':
root = new DataNode(DataType.List);
while((node = parse( index, json )) != null){
while(end.i != 1 && (node = parse(in, end)) != null){
root.add( node );
}
end.i = 0;
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;
StringBuilder str = new StringBuilder();
while((c=(char)in.read()) != (char)-1 && c != '\"')
str.append(c);
root.set(str.toString());
break;
// Parse Number
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)=='}'){
StringBuilder num = new StringBuilder().append(c);
while((c=(char)in.read()) != (char)-1 && !Character.isWhitespace(c) &&
c != ',' && c != '='){
if(c == ']' || c == '}'){
end.i = 1;
break;
}
root.set( c+json.substring(index.i, next_index) );
index.i = next_index;
num.append(c);
}
root.set(num.toString());
break;
}
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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
@ -33,20 +34,69 @@ import zutil.parser.json.JSONParser;
public class JSONTest{
@Test
public void JSONParser() {
JSONParser parser = new JSONParser();
@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
public void complexMap() {
String json = "{" +
"\"test1\": 1234," +
"\"TEST1\": 5678," +
"\"test3\": 1234.99," +
"\"test4\": \"91011\"," +
"\"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( 1234 == data.get("test1").getInt() );
assert( 5678 == data.get("TEST1").getInt() );