diff --git a/src/zutil/parser/json/JSONParser.java b/src/zutil/parser/json/JSONParser.java index 11e7ac7..f185809 100644 --- a/src/zutil/parser/json/JSONParser.java +++ b/src/zutil/parser/json/JSONParser.java @@ -26,29 +26,95 @@ import zutil.parser.DataNode; import zutil.parser.DataNode.DataType; import zutil.struct.MutableInt; +import java.io.BufferedReader; + /** * This is a JSON parser class * * @author Ziver */ public class JSONParser{ + protected Readable in; + + public JSONParser(Readable in){ + this.in = in; + } + + /** + * Starts parsing from the InputStream. + * This method will block until one root tree has been parsed. + * + * @return a DataNode object representing the input JSON + */ + public DataNode read(){ + return parse(in); + } /** - * Starts parsing + * Starts parsing from a string * * @param json is the JSON String to parse - * @return a JSONNode object that is the root of the JSON + * @return a DataNode object representing the JSON in the input String */ - public DataNode read(String json){ + public static DataNode read(String json){ return parse(new MutableInt(), new StringBuilder(json)); } - + /** + * This is the real recursive parsing method + */ + protected static DataNode parse(Readable in){ + 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