json-simple/vendor/json-simple-1.1.1/src/main/java/org/json/simple/parser/JSONParser.java

Code
Comments
Other
Rev Date Author Line
50 26 Feb 19 nicklas 1 /*
50 26 Feb 19 nicklas 2  * $Id: JSONParser.java,v 1.1 2006/04/15 14:10:48 platform Exp $
50 26 Feb 19 nicklas 3  * Created on 2006-4-15
50 26 Feb 19 nicklas 4  */
50 26 Feb 19 nicklas 5 package org.json.simple.parser;
50 26 Feb 19 nicklas 6
50 26 Feb 19 nicklas 7 import java.io.IOException;
50 26 Feb 19 nicklas 8 import java.io.Reader;
50 26 Feb 19 nicklas 9 import java.io.StringReader;
50 26 Feb 19 nicklas 10 import java.util.LinkedList;
50 26 Feb 19 nicklas 11 import java.util.List;
50 26 Feb 19 nicklas 12 import java.util.Map;
50 26 Feb 19 nicklas 13
50 26 Feb 19 nicklas 14 import org.json.simple.JSONArray;
50 26 Feb 19 nicklas 15 import org.json.simple.JSONObject;
50 26 Feb 19 nicklas 16
50 26 Feb 19 nicklas 17
50 26 Feb 19 nicklas 18 /**
50 26 Feb 19 nicklas 19  * Parser for JSON text. Please note that JSONParser is NOT thread-safe.
50 26 Feb 19 nicklas 20  * 
50 26 Feb 19 nicklas 21  * @author FangYidong<fangyidong@yahoo.com.cn>
50 26 Feb 19 nicklas 22  */
50 26 Feb 19 nicklas 23 public class JSONParser {
50 26 Feb 19 nicklas 24   public static final int S_INIT=0;
50 26 Feb 19 nicklas 25   public static final int S_IN_FINISHED_VALUE=1;//string,number,boolean,null,object,array
50 26 Feb 19 nicklas 26   public static final int S_IN_OBJECT=2;
50 26 Feb 19 nicklas 27   public static final int S_IN_ARRAY=3;
50 26 Feb 19 nicklas 28   public static final int S_PASSED_PAIR_KEY=4;
50 26 Feb 19 nicklas 29   public static final int S_IN_PAIR_VALUE=5;
50 26 Feb 19 nicklas 30   public static final int S_END=6;
50 26 Feb 19 nicklas 31   public static final int S_IN_ERROR=-1;
50 26 Feb 19 nicklas 32   
50 26 Feb 19 nicklas 33   private LinkedList handlerStatusStack;
50 26 Feb 19 nicklas 34   private Yylex lexer = new Yylex((Reader)null);
50 26 Feb 19 nicklas 35   private Yytoken token = null;
50 26 Feb 19 nicklas 36   private int status = S_INIT;
50 26 Feb 19 nicklas 37   
50 26 Feb 19 nicklas 38   private int peekStatus(LinkedList statusStack){
50 26 Feb 19 nicklas 39     if(statusStack.size()==0)
50 26 Feb 19 nicklas 40       return -1;
50 26 Feb 19 nicklas 41     Integer status=(Integer)statusStack.getFirst();
50 26 Feb 19 nicklas 42     return status.intValue();
50 26 Feb 19 nicklas 43   }
50 26 Feb 19 nicklas 44   
50 26 Feb 19 nicklas 45     /**
50 26 Feb 19 nicklas 46      *  Reset the parser to the initial state without resetting the underlying reader.
50 26 Feb 19 nicklas 47      *
50 26 Feb 19 nicklas 48      */
50 26 Feb 19 nicklas 49     public void reset(){
50 26 Feb 19 nicklas 50         token = null;
50 26 Feb 19 nicklas 51         status = S_INIT;
50 26 Feb 19 nicklas 52         handlerStatusStack = null;
50 26 Feb 19 nicklas 53     }
50 26 Feb 19 nicklas 54     
50 26 Feb 19 nicklas 55     /**
50 26 Feb 19 nicklas 56      * Reset the parser to the initial state with a new character reader.
50 26 Feb 19 nicklas 57      * 
50 26 Feb 19 nicklas 58      * @param in - The new character reader.
50 26 Feb 19 nicklas 59      * @throws IOException
50 26 Feb 19 nicklas 60      * @throws ParseException
50 26 Feb 19 nicklas 61      */
50 26 Feb 19 nicklas 62   public void reset(Reader in){
50 26 Feb 19 nicklas 63     lexer.yyreset(in);
50 26 Feb 19 nicklas 64     reset();
50 26 Feb 19 nicklas 65   }
50 26 Feb 19 nicklas 66   
50 26 Feb 19 nicklas 67   /**
50 26 Feb 19 nicklas 68    * @return The position of the beginning of the current token.
50 26 Feb 19 nicklas 69    */
50 26 Feb 19 nicklas 70   public int getPosition(){
50 26 Feb 19 nicklas 71     return lexer.getPosition();
50 26 Feb 19 nicklas 72   }
50 26 Feb 19 nicklas 73   
50 26 Feb 19 nicklas 74   public Object parse(String s) throws ParseException{
50 26 Feb 19 nicklas 75     return parse(s, (ContainerFactory)null);
50 26 Feb 19 nicklas 76   }
50 26 Feb 19 nicklas 77   
50 26 Feb 19 nicklas 78   public Object parse(String s, ContainerFactory containerFactory) throws ParseException{
50 26 Feb 19 nicklas 79     StringReader in=new StringReader(s);
50 26 Feb 19 nicklas 80     try{
50 26 Feb 19 nicklas 81       return parse(in, containerFactory);
50 26 Feb 19 nicklas 82     }
50 26 Feb 19 nicklas 83     catch(IOException ie){
50 26 Feb 19 nicklas 84       /*
50 26 Feb 19 nicklas 85        * Actually it will never happen.
50 26 Feb 19 nicklas 86        */
50 26 Feb 19 nicklas 87       throw new ParseException(-1, ParseException.ERROR_UNEXPECTED_EXCEPTION, ie);
50 26 Feb 19 nicklas 88     }
50 26 Feb 19 nicklas 89   }
50 26 Feb 19 nicklas 90   
50 26 Feb 19 nicklas 91   public Object parse(Reader in) throws IOException, ParseException{
50 26 Feb 19 nicklas 92     return parse(in, (ContainerFactory)null);
50 26 Feb 19 nicklas 93   }
50 26 Feb 19 nicklas 94   
50 26 Feb 19 nicklas 95   /**
50 26 Feb 19 nicklas 96    * Parse JSON text into java object from the input source.
50 26 Feb 19 nicklas 97    *   
50 26 Feb 19 nicklas 98    * @param in
50 26 Feb 19 nicklas 99      * @param containerFactory - Use this factory to createyour own JSON object and JSON array containers.
50 26 Feb 19 nicklas 100    * @return Instance of the following:
50 26 Feb 19 nicklas 101    *  org.json.simple.JSONObject,
50 26 Feb 19 nicklas 102    *   org.json.simple.JSONArray,
50 26 Feb 19 nicklas 103    *   java.lang.String,
50 26 Feb 19 nicklas 104    *   java.lang.Number,
50 26 Feb 19 nicklas 105    *   java.lang.Boolean,
50 26 Feb 19 nicklas 106    *   null
50 26 Feb 19 nicklas 107    * 
50 26 Feb 19 nicklas 108    * @throws IOException
50 26 Feb 19 nicklas 109    * @throws ParseException
50 26 Feb 19 nicklas 110    */
50 26 Feb 19 nicklas 111   public Object parse(Reader in, ContainerFactory containerFactory) throws IOException, ParseException{
50 26 Feb 19 nicklas 112     reset(in);
50 26 Feb 19 nicklas 113     LinkedList statusStack = new LinkedList();
50 26 Feb 19 nicklas 114     LinkedList valueStack = new LinkedList();
50 26 Feb 19 nicklas 115     
50 26 Feb 19 nicklas 116     try{
50 26 Feb 19 nicklas 117       do{
50 26 Feb 19 nicklas 118         nextToken();
50 26 Feb 19 nicklas 119         switch(status){
50 26 Feb 19 nicklas 120         case S_INIT:
50 26 Feb 19 nicklas 121           switch(token.type){
50 26 Feb 19 nicklas 122           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 123             status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 124             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 125             valueStack.addFirst(token.value);
50 26 Feb 19 nicklas 126             break;
50 26 Feb 19 nicklas 127           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 128             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 129             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 130             valueStack.addFirst(createObjectContainer(containerFactory));
50 26 Feb 19 nicklas 131             break;
50 26 Feb 19 nicklas 132           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 133             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 134             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 135             valueStack.addFirst(createArrayContainer(containerFactory));
50 26 Feb 19 nicklas 136             break;
50 26 Feb 19 nicklas 137           default:
50 26 Feb 19 nicklas 138             status=S_IN_ERROR;
50 26 Feb 19 nicklas 139           }//inner switch
50 26 Feb 19 nicklas 140           break;
50 26 Feb 19 nicklas 141           
50 26 Feb 19 nicklas 142         case S_IN_FINISHED_VALUE:
50 26 Feb 19 nicklas 143           if(token.type==Yytoken.TYPE_EOF)
50 26 Feb 19 nicklas 144             return valueStack.removeFirst();
50 26 Feb 19 nicklas 145           else
50 26 Feb 19 nicklas 146             throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 147           
50 26 Feb 19 nicklas 148         case S_IN_OBJECT:
50 26 Feb 19 nicklas 149           switch(token.type){
50 26 Feb 19 nicklas 150           case Yytoken.TYPE_COMMA:
50 26 Feb 19 nicklas 151             break;
50 26 Feb 19 nicklas 152           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 153             if(token.value instanceof String){
50 26 Feb 19 nicklas 154               String key=(String)token.value;
50 26 Feb 19 nicklas 155               valueStack.addFirst(key);
50 26 Feb 19 nicklas 156               status=S_PASSED_PAIR_KEY;
50 26 Feb 19 nicklas 157               statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 158             }
50 26 Feb 19 nicklas 159             else{
50 26 Feb 19 nicklas 160               status=S_IN_ERROR;
50 26 Feb 19 nicklas 161             }
50 26 Feb 19 nicklas 162             break;
50 26 Feb 19 nicklas 163           case Yytoken.TYPE_RIGHT_BRACE:
50 26 Feb 19 nicklas 164             if(valueStack.size()>1){
50 26 Feb 19 nicklas 165               statusStack.removeFirst();
50 26 Feb 19 nicklas 166               valueStack.removeFirst();
50 26 Feb 19 nicklas 167               status=peekStatus(statusStack);
50 26 Feb 19 nicklas 168             }
50 26 Feb 19 nicklas 169             else{
50 26 Feb 19 nicklas 170               status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 171             }
50 26 Feb 19 nicklas 172             break;
50 26 Feb 19 nicklas 173           default:
50 26 Feb 19 nicklas 174             status=S_IN_ERROR;
50 26 Feb 19 nicklas 175             break;
50 26 Feb 19 nicklas 176           }//inner switch
50 26 Feb 19 nicklas 177           break;
50 26 Feb 19 nicklas 178           
50 26 Feb 19 nicklas 179         case S_PASSED_PAIR_KEY:
50 26 Feb 19 nicklas 180           switch(token.type){
50 26 Feb 19 nicklas 181           case Yytoken.TYPE_COLON:
50 26 Feb 19 nicklas 182             break;
50 26 Feb 19 nicklas 183           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 184             statusStack.removeFirst();
50 26 Feb 19 nicklas 185             String key=(String)valueStack.removeFirst();
50 26 Feb 19 nicklas 186             Map parent=(Map)valueStack.getFirst();
50 26 Feb 19 nicklas 187             parent.put(key,token.value);
50 26 Feb 19 nicklas 188             status=peekStatus(statusStack);
50 26 Feb 19 nicklas 189             break;
50 26 Feb 19 nicklas 190           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 191             statusStack.removeFirst();
50 26 Feb 19 nicklas 192             key=(String)valueStack.removeFirst();
50 26 Feb 19 nicklas 193             parent=(Map)valueStack.getFirst();
50 26 Feb 19 nicklas 194             List newArray=createArrayContainer(containerFactory);
50 26 Feb 19 nicklas 195             parent.put(key,newArray);
50 26 Feb 19 nicklas 196             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 197             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 198             valueStack.addFirst(newArray);
50 26 Feb 19 nicklas 199             break;
50 26 Feb 19 nicklas 200           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 201             statusStack.removeFirst();
50 26 Feb 19 nicklas 202             key=(String)valueStack.removeFirst();
50 26 Feb 19 nicklas 203             parent=(Map)valueStack.getFirst();
50 26 Feb 19 nicklas 204             Map newObject=createObjectContainer(containerFactory);
50 26 Feb 19 nicklas 205             parent.put(key,newObject);
50 26 Feb 19 nicklas 206             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 207             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 208             valueStack.addFirst(newObject);
50 26 Feb 19 nicklas 209             break;
50 26 Feb 19 nicklas 210           default:
50 26 Feb 19 nicklas 211             status=S_IN_ERROR;
50 26 Feb 19 nicklas 212           }
50 26 Feb 19 nicklas 213           break;
50 26 Feb 19 nicklas 214           
50 26 Feb 19 nicklas 215         case S_IN_ARRAY:
50 26 Feb 19 nicklas 216           switch(token.type){
50 26 Feb 19 nicklas 217           case Yytoken.TYPE_COMMA:
50 26 Feb 19 nicklas 218             break;
50 26 Feb 19 nicklas 219           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 220             List val=(List)valueStack.getFirst();
50 26 Feb 19 nicklas 221             val.add(token.value);
50 26 Feb 19 nicklas 222             break;
50 26 Feb 19 nicklas 223           case Yytoken.TYPE_RIGHT_SQUARE:
50 26 Feb 19 nicklas 224             if(valueStack.size()>1){
50 26 Feb 19 nicklas 225               statusStack.removeFirst();
50 26 Feb 19 nicklas 226               valueStack.removeFirst();
50 26 Feb 19 nicklas 227               status=peekStatus(statusStack);
50 26 Feb 19 nicklas 228             }
50 26 Feb 19 nicklas 229             else{
50 26 Feb 19 nicklas 230               status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 231             }
50 26 Feb 19 nicklas 232             break;
50 26 Feb 19 nicklas 233           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 234             val=(List)valueStack.getFirst();
50 26 Feb 19 nicklas 235             Map newObject=createObjectContainer(containerFactory);
50 26 Feb 19 nicklas 236             val.add(newObject);
50 26 Feb 19 nicklas 237             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 238             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 239             valueStack.addFirst(newObject);
50 26 Feb 19 nicklas 240             break;
50 26 Feb 19 nicklas 241           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 242             val=(List)valueStack.getFirst();
50 26 Feb 19 nicklas 243             List newArray=createArrayContainer(containerFactory);
50 26 Feb 19 nicklas 244             val.add(newArray);
50 26 Feb 19 nicklas 245             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 246             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 247             valueStack.addFirst(newArray);
50 26 Feb 19 nicklas 248             break;
50 26 Feb 19 nicklas 249           default:
50 26 Feb 19 nicklas 250             status=S_IN_ERROR;
50 26 Feb 19 nicklas 251           }//inner switch
50 26 Feb 19 nicklas 252           break;
50 26 Feb 19 nicklas 253         case S_IN_ERROR:
50 26 Feb 19 nicklas 254           throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 255         }//switch
50 26 Feb 19 nicklas 256         if(status==S_IN_ERROR){
50 26 Feb 19 nicklas 257           throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 258         }
50 26 Feb 19 nicklas 259       }while(token.type!=Yytoken.TYPE_EOF);
50 26 Feb 19 nicklas 260     }
50 26 Feb 19 nicklas 261     catch(IOException ie){
50 26 Feb 19 nicklas 262       throw ie;
50 26 Feb 19 nicklas 263     }
50 26 Feb 19 nicklas 264     
50 26 Feb 19 nicklas 265     throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 266   }
50 26 Feb 19 nicklas 267   
50 26 Feb 19 nicklas 268   private void nextToken() throws ParseException, IOException{
50 26 Feb 19 nicklas 269     token = lexer.yylex();
50 26 Feb 19 nicklas 270     if(token == null)
50 26 Feb 19 nicklas 271       token = new Yytoken(Yytoken.TYPE_EOF, null);
50 26 Feb 19 nicklas 272   }
50 26 Feb 19 nicklas 273   
50 26 Feb 19 nicklas 274   private Map createObjectContainer(ContainerFactory containerFactory){
50 26 Feb 19 nicklas 275     if(containerFactory == null)
50 26 Feb 19 nicklas 276       return new JSONObject();
50 26 Feb 19 nicklas 277     Map m = containerFactory.createObjectContainer();
50 26 Feb 19 nicklas 278     
50 26 Feb 19 nicklas 279     if(m == null)
50 26 Feb 19 nicklas 280       return new JSONObject();
50 26 Feb 19 nicklas 281     return m;
50 26 Feb 19 nicklas 282   }
50 26 Feb 19 nicklas 283   
50 26 Feb 19 nicklas 284   private List createArrayContainer(ContainerFactory containerFactory){
50 26 Feb 19 nicklas 285     if(containerFactory == null)
50 26 Feb 19 nicklas 286       return new JSONArray();
50 26 Feb 19 nicklas 287     List l = containerFactory.creatArrayContainer();
50 26 Feb 19 nicklas 288     
50 26 Feb 19 nicklas 289     if(l == null)
50 26 Feb 19 nicklas 290       return new JSONArray();
50 26 Feb 19 nicklas 291     return l;
50 26 Feb 19 nicklas 292   }
50 26 Feb 19 nicklas 293   
50 26 Feb 19 nicklas 294   public void parse(String s, ContentHandler contentHandler) throws ParseException{
50 26 Feb 19 nicklas 295     parse(s, contentHandler, false);
50 26 Feb 19 nicklas 296   }
50 26 Feb 19 nicklas 297   
50 26 Feb 19 nicklas 298   public void parse(String s, ContentHandler contentHandler, boolean isResume) throws ParseException{
50 26 Feb 19 nicklas 299     StringReader in=new StringReader(s);
50 26 Feb 19 nicklas 300     try{
50 26 Feb 19 nicklas 301       parse(in, contentHandler, isResume);
50 26 Feb 19 nicklas 302     }
50 26 Feb 19 nicklas 303     catch(IOException ie){
50 26 Feb 19 nicklas 304       /*
50 26 Feb 19 nicklas 305        * Actually it will never happen.
50 26 Feb 19 nicklas 306        */
50 26 Feb 19 nicklas 307       throw new ParseException(-1, ParseException.ERROR_UNEXPECTED_EXCEPTION, ie);
50 26 Feb 19 nicklas 308     }
50 26 Feb 19 nicklas 309   }
50 26 Feb 19 nicklas 310   
50 26 Feb 19 nicklas 311   public void parse(Reader in, ContentHandler contentHandler) throws IOException, ParseException{
50 26 Feb 19 nicklas 312     parse(in, contentHandler, false);
50 26 Feb 19 nicklas 313   }
50 26 Feb 19 nicklas 314   
50 26 Feb 19 nicklas 315   /**
50 26 Feb 19 nicklas 316    * Stream processing of JSON text.
50 26 Feb 19 nicklas 317    * 
50 26 Feb 19 nicklas 318    * @see ContentHandler
50 26 Feb 19 nicklas 319    * 
50 26 Feb 19 nicklas 320    * @param in
50 26 Feb 19 nicklas 321    * @param contentHandler
50 26 Feb 19 nicklas 322    * @param isResume - Indicates if it continues previous parsing operation.
50 26 Feb 19 nicklas 323      *                   If set to true, resume parsing the old stream, and parameter 'in' will be ignored. 
50 26 Feb 19 nicklas 324    *                   If this method is called for the first time in this instance, isResume will be ignored.
50 26 Feb 19 nicklas 325    * 
50 26 Feb 19 nicklas 326    * @throws IOException
50 26 Feb 19 nicklas 327    * @throws ParseException
50 26 Feb 19 nicklas 328    */
50 26 Feb 19 nicklas 329   public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException{
50 26 Feb 19 nicklas 330     if(!isResume){
50 26 Feb 19 nicklas 331       reset(in);
50 26 Feb 19 nicklas 332       handlerStatusStack = new LinkedList();
50 26 Feb 19 nicklas 333     }
50 26 Feb 19 nicklas 334     else{
50 26 Feb 19 nicklas 335       if(handlerStatusStack == null){
50 26 Feb 19 nicklas 336         isResume = false;
50 26 Feb 19 nicklas 337         reset(in);
50 26 Feb 19 nicklas 338         handlerStatusStack = new LinkedList();
50 26 Feb 19 nicklas 339       }
50 26 Feb 19 nicklas 340     }
50 26 Feb 19 nicklas 341     
50 26 Feb 19 nicklas 342     LinkedList statusStack = handlerStatusStack;  
50 26 Feb 19 nicklas 343     
50 26 Feb 19 nicklas 344     try{
50 26 Feb 19 nicklas 345       do{
50 26 Feb 19 nicklas 346         switch(status){
50 26 Feb 19 nicklas 347         case S_INIT:
50 26 Feb 19 nicklas 348           contentHandler.startJSON();
50 26 Feb 19 nicklas 349           nextToken();
50 26 Feb 19 nicklas 350           switch(token.type){
50 26 Feb 19 nicklas 351           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 352             status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 353             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 354             if(!contentHandler.primitive(token.value))
50 26 Feb 19 nicklas 355               return;
50 26 Feb 19 nicklas 356             break;
50 26 Feb 19 nicklas 357           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 358             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 359             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 360             if(!contentHandler.startObject())
50 26 Feb 19 nicklas 361               return;
50 26 Feb 19 nicklas 362             break;
50 26 Feb 19 nicklas 363           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 364             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 365             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 366             if(!contentHandler.startArray())
50 26 Feb 19 nicklas 367               return;
50 26 Feb 19 nicklas 368             break;
50 26 Feb 19 nicklas 369           default:
50 26 Feb 19 nicklas 370             status=S_IN_ERROR;
50 26 Feb 19 nicklas 371           }//inner switch
50 26 Feb 19 nicklas 372           break;
50 26 Feb 19 nicklas 373           
50 26 Feb 19 nicklas 374         case S_IN_FINISHED_VALUE:
50 26 Feb 19 nicklas 375           nextToken();
50 26 Feb 19 nicklas 376           if(token.type==Yytoken.TYPE_EOF){
50 26 Feb 19 nicklas 377             contentHandler.endJSON();
50 26 Feb 19 nicklas 378             status = S_END;
50 26 Feb 19 nicklas 379             return;
50 26 Feb 19 nicklas 380           }
50 26 Feb 19 nicklas 381           else{
50 26 Feb 19 nicklas 382             status = S_IN_ERROR;
50 26 Feb 19 nicklas 383             throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 384           }
50 26 Feb 19 nicklas 385       
50 26 Feb 19 nicklas 386         case S_IN_OBJECT:
50 26 Feb 19 nicklas 387           nextToken();
50 26 Feb 19 nicklas 388           switch(token.type){
50 26 Feb 19 nicklas 389           case Yytoken.TYPE_COMMA:
50 26 Feb 19 nicklas 390             break;
50 26 Feb 19 nicklas 391           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 392             if(token.value instanceof String){
50 26 Feb 19 nicklas 393               String key=(String)token.value;
50 26 Feb 19 nicklas 394               status=S_PASSED_PAIR_KEY;
50 26 Feb 19 nicklas 395               statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 396               if(!contentHandler.startObjectEntry(key))
50 26 Feb 19 nicklas 397                 return;
50 26 Feb 19 nicklas 398             }
50 26 Feb 19 nicklas 399             else{
50 26 Feb 19 nicklas 400               status=S_IN_ERROR;
50 26 Feb 19 nicklas 401             }
50 26 Feb 19 nicklas 402             break;
50 26 Feb 19 nicklas 403           case Yytoken.TYPE_RIGHT_BRACE:
50 26 Feb 19 nicklas 404             if(statusStack.size()>1){
50 26 Feb 19 nicklas 405               statusStack.removeFirst();
50 26 Feb 19 nicklas 406               status=peekStatus(statusStack);
50 26 Feb 19 nicklas 407             }
50 26 Feb 19 nicklas 408             else{
50 26 Feb 19 nicklas 409               status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 410             }
50 26 Feb 19 nicklas 411             if(!contentHandler.endObject())
50 26 Feb 19 nicklas 412               return;
50 26 Feb 19 nicklas 413             break;
50 26 Feb 19 nicklas 414           default:
50 26 Feb 19 nicklas 415             status=S_IN_ERROR;
50 26 Feb 19 nicklas 416             break;
50 26 Feb 19 nicklas 417           }//inner switch
50 26 Feb 19 nicklas 418           break;
50 26 Feb 19 nicklas 419           
50 26 Feb 19 nicklas 420         case S_PASSED_PAIR_KEY:
50 26 Feb 19 nicklas 421           nextToken();
50 26 Feb 19 nicklas 422           switch(token.type){
50 26 Feb 19 nicklas 423           case Yytoken.TYPE_COLON:
50 26 Feb 19 nicklas 424             break;
50 26 Feb 19 nicklas 425           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 426             statusStack.removeFirst();
50 26 Feb 19 nicklas 427             status=peekStatus(statusStack);
50 26 Feb 19 nicklas 428             if(!contentHandler.primitive(token.value))
50 26 Feb 19 nicklas 429               return;
50 26 Feb 19 nicklas 430             if(!contentHandler.endObjectEntry())
50 26 Feb 19 nicklas 431               return;
50 26 Feb 19 nicklas 432             break;
50 26 Feb 19 nicklas 433           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 434             statusStack.removeFirst();
50 26 Feb 19 nicklas 435             statusStack.addFirst(new Integer(S_IN_PAIR_VALUE));
50 26 Feb 19 nicklas 436             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 437             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 438             if(!contentHandler.startArray())
50 26 Feb 19 nicklas 439               return;
50 26 Feb 19 nicklas 440             break;
50 26 Feb 19 nicklas 441           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 442             statusStack.removeFirst();
50 26 Feb 19 nicklas 443             statusStack.addFirst(new Integer(S_IN_PAIR_VALUE));
50 26 Feb 19 nicklas 444             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 445             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 446             if(!contentHandler.startObject())
50 26 Feb 19 nicklas 447               return;
50 26 Feb 19 nicklas 448             break;
50 26 Feb 19 nicklas 449           default:
50 26 Feb 19 nicklas 450             status=S_IN_ERROR;
50 26 Feb 19 nicklas 451           }
50 26 Feb 19 nicklas 452           break;
50 26 Feb 19 nicklas 453         
50 26 Feb 19 nicklas 454         case S_IN_PAIR_VALUE:
50 26 Feb 19 nicklas 455           /*
50 26 Feb 19 nicklas 456            * S_IN_PAIR_VALUE is just a marker to indicate the end of an object entry, it doesn't proccess any token,
50 26 Feb 19 nicklas 457            * therefore delay consuming token until next round.
50 26 Feb 19 nicklas 458            */
50 26 Feb 19 nicklas 459           statusStack.removeFirst();
50 26 Feb 19 nicklas 460           status = peekStatus(statusStack);
50 26 Feb 19 nicklas 461           if(!contentHandler.endObjectEntry())
50 26 Feb 19 nicklas 462             return;
50 26 Feb 19 nicklas 463           break;
50 26 Feb 19 nicklas 464           
50 26 Feb 19 nicklas 465         case S_IN_ARRAY:
50 26 Feb 19 nicklas 466           nextToken();
50 26 Feb 19 nicklas 467           switch(token.type){
50 26 Feb 19 nicklas 468           case Yytoken.TYPE_COMMA:
50 26 Feb 19 nicklas 469             break;
50 26 Feb 19 nicklas 470           case Yytoken.TYPE_VALUE:
50 26 Feb 19 nicklas 471             if(!contentHandler.primitive(token.value))
50 26 Feb 19 nicklas 472               return;
50 26 Feb 19 nicklas 473             break;
50 26 Feb 19 nicklas 474           case Yytoken.TYPE_RIGHT_SQUARE:
50 26 Feb 19 nicklas 475             if(statusStack.size()>1){
50 26 Feb 19 nicklas 476               statusStack.removeFirst();
50 26 Feb 19 nicklas 477               status=peekStatus(statusStack);
50 26 Feb 19 nicklas 478             }
50 26 Feb 19 nicklas 479             else{
50 26 Feb 19 nicklas 480               status=S_IN_FINISHED_VALUE;
50 26 Feb 19 nicklas 481             }
50 26 Feb 19 nicklas 482             if(!contentHandler.endArray())
50 26 Feb 19 nicklas 483               return;
50 26 Feb 19 nicklas 484             break;
50 26 Feb 19 nicklas 485           case Yytoken.TYPE_LEFT_BRACE:
50 26 Feb 19 nicklas 486             status=S_IN_OBJECT;
50 26 Feb 19 nicklas 487             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 488             if(!contentHandler.startObject())
50 26 Feb 19 nicklas 489               return;
50 26 Feb 19 nicklas 490             break;
50 26 Feb 19 nicklas 491           case Yytoken.TYPE_LEFT_SQUARE:
50 26 Feb 19 nicklas 492             status=S_IN_ARRAY;
50 26 Feb 19 nicklas 493             statusStack.addFirst(new Integer(status));
50 26 Feb 19 nicklas 494             if(!contentHandler.startArray())
50 26 Feb 19 nicklas 495               return;
50 26 Feb 19 nicklas 496             break;
50 26 Feb 19 nicklas 497           default:
50 26 Feb 19 nicklas 498             status=S_IN_ERROR;
50 26 Feb 19 nicklas 499           }//inner switch
50 26 Feb 19 nicklas 500           break;
50 26 Feb 19 nicklas 501           
50 26 Feb 19 nicklas 502         case S_END:
50 26 Feb 19 nicklas 503           return;
50 26 Feb 19 nicklas 504           
50 26 Feb 19 nicklas 505         case S_IN_ERROR:
50 26 Feb 19 nicklas 506           throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 507         }//switch
50 26 Feb 19 nicklas 508         if(status==S_IN_ERROR){
50 26 Feb 19 nicklas 509           throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 510         }
50 26 Feb 19 nicklas 511       }while(token.type!=Yytoken.TYPE_EOF);
50 26 Feb 19 nicklas 512     }
50 26 Feb 19 nicklas 513     catch(IOException ie){
50 26 Feb 19 nicklas 514       status = S_IN_ERROR;
50 26 Feb 19 nicklas 515       throw ie;
50 26 Feb 19 nicklas 516     }
50 26 Feb 19 nicklas 517     catch(ParseException pe){
50 26 Feb 19 nicklas 518       status = S_IN_ERROR;
50 26 Feb 19 nicklas 519       throw pe;
50 26 Feb 19 nicklas 520     }
50 26 Feb 19 nicklas 521     catch(RuntimeException re){
50 26 Feb 19 nicklas 522       status = S_IN_ERROR;
50 26 Feb 19 nicklas 523       throw re;
50 26 Feb 19 nicklas 524     }
50 26 Feb 19 nicklas 525     catch(Error e){
50 26 Feb 19 nicklas 526       status = S_IN_ERROR;
50 26 Feb 19 nicklas 527       throw e;
50 26 Feb 19 nicklas 528     }
50 26 Feb 19 nicklas 529     
50 26 Feb 19 nicklas 530     status = S_IN_ERROR;
50 26 Feb 19 nicklas 531     throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
50 26 Feb 19 nicklas 532   }
50 26 Feb 19 nicklas 533 }