Exemplo n.º 1
0
 /**
    * Construct a JSONArray from a JSONTokener.
    * @param x A JSONTokener
    * @exception Json.InvalidJsonException A JSONArray must start with '['
    * @exception Json.InvalidJsonException Expected a ',' or ']'
    */
 public JSONArray(JSONTokener x)
     : this()
 {
     if (x.nextClean() != '[')
       throw x.syntaxError("A JSONArray must start with '['");
     if (x.nextClean() == ']')
       return;
     x.back();
     while (true) {
       if (x.nextClean() == ',') {
     x.back();
     myArrayList.Add(null);
       } else {
     x.back();
     myArrayList.Add(x.nextValue());
       }
       switch (x.nextClean()) {
       case ',':
     if (x.nextClean() == ']'){
       if((x.getOptions() & JSONObject.OPTION_TRAILING_COMMAS)==0){
        // 2013-05-24 -- Peter O. Disallow trailing comma.
        throw x.syntaxError("Trailing comma");
       } else {
     return;
       }
     }
     x.back();
     break;
       case ']':
     return;
       default:
     throw x.syntaxError("Expected a ',' or ']'");
       }
     }
 }
Exemplo n.º 2
0
 /**
    * Construct a JSONObject from a JSONTokener.
    * @param x A JSONTokener _object containing the source _string.
    * @ if there is a syntax error in the source _string.
    */
 public JSONObject(JSONTokener x)
     : this()
 {
     int c;
     string key;
     if (x.next() == '%') {
       x.unescape();
     }
     x.back();
     if (x.nextClean() != '{')
       throw x.syntaxError("A JSONObject must begin with '{'");
     while (true) {
       addCommentIfAny(x);
       c = x.nextClean();
       switch (c) {
       case -1:
     throw x.syntaxError("A JSONObject must end with '}'");
       case '}':
     return;
       default:
     x.back();
     addCommentIfAny(x);
     key = x.nextValue().ToString();
     if((x.getOptions() & OPTION_NO_DUPLICATES)!=0 &&
     myHashMap.ContainsKey(key)){
       throw x.syntaxError("Key already exists: "+key);
     }
     break;
       }
       addCommentIfAny(x);
       if (x.nextClean() != ':')
     throw x.syntaxError("Expected a ':' after a key");
       // NOTE: Will overwrite existing value. --Peter O.
       addCommentIfAny(x);
       myHashMap.Add(key, x.nextValue());
       addCommentIfAny(x);
       switch (x.nextClean()) {
       case ',':
     addCommentIfAny(x);
     if (x.nextClean() == '}'){
       if((x.getOptions() & OPTION_TRAILING_COMMAS)==0){
       // 2013-05-24 -- Peter O. Disallow trailing comma.
       throw x.syntaxError("Trailing comma");
       } else {
     return;
       }
     }
     x.back();
     break;
       case '}':
     return;
       default:
     throw x.syntaxError("Expected a ',' or '}'");
       }
     }
 }