Exemplo n.º 1
0
  /**
   * Construct a JSONObject from a JSONTokener.
   *
   * @param x A JSONTokener object containing the source string.
   * @throws JSONException If there is a syntax error in the source string or a duplicated key.
   */
  public JSONObject(JSONTokener x) throws JSONException {
    this();
    char c;
    String key;

    if (x.nextClean() != '{') {
      throw x.syntaxError("A JSONObject text must begin with '{'");
    }
    for (; ; ) {
      c = x.nextClean();
      switch (c) {
        case 0:
          throw x.syntaxError("A JSONObject text must end with '}'");
        case '}':
          return;
        default:
          x.back();
          key = x.nextValue().toString();
      }

      /*
       * The key is followed by ':'. We will also tolerate '=' or '=>'.
       */

      c = x.nextClean();
      if (c == '=') {
        if (x.next() != '>') {
          x.back();
        }
      } else if (c != ':') {
        throw x.syntaxError("Expected a ':' after a key");
      }
      putOnce(key, x.nextValue());

      /*
       * Pairs are separated by ','. We will also tolerate ';'.
       */

      switch (x.nextClean()) {
        case ';':
        case ',':
          if (x.nextClean() == '}') {
            return;
          }
          x.back();
          break;
        case '}':
          return;
        default:
          throw x.syntaxError("Expected a ',' or '}'");
      }
    }
  }
Exemplo n.º 2
0
 /**
  * Construct a JSONArray from a JSONTokener.
  *
  * @param x A JSONTokener
  * @throws JSONException If there is a syntax error.
  */
 public JSONArray(JSONTokener x) throws JSONException {
   this();
   if (x.nextClean() != '[') throw x.syntaxError("A JSONArray text must start with '['");
   if (x.nextClean() != ']') {
     x.back();
     for (; ; ) {
       if (x.nextClean() == ',') {
         x.back();
         myArrayList.add(JSONObject.NULL);
       } else {
         x.back();
         myArrayList.add(x.nextValue());
       }
       switch (x.nextClean()) {
         case ';':
         case ',':
           if (x.nextClean() == ']') return;
           x.back();
           break;
         case ']':
           return;
         default:
           throw x.syntaxError("Expected a ',' or ']'");
       }
     }
   }
 }
Exemplo n.º 3
0
 /**
  * Construct a JSONArray from a JSONTokener.
  *
  * @param x A JSONTokener
  * @throws JSONException If there is a syntax error.
  * @throws IOException If error
  */
 public JSONArray(JSONTokener x) throws JSONException, IOException {
   this();
   if (x.nextClean() != '[') {
     throw new JSONException(x, "A JSONArray text must start with '['");
   }
   if (x.nextClean() != ']') {
     x.back();
     for (; ; ) {
       if (x.nextClean() == ',') {
         x.back();
         this.myArrayList.add(JSONObject.NULL);
       } else {
         x.back();
         this.myArrayList.add(x.nextValue());
       }
       switch (x.nextClean()) {
         case ',':
           if (x.nextClean() == ']') {
             return;
           }
           x.back();
           break;
         case ']':
           return;
         default:
           throw new JSONException(x, "Expected a ',' or ']'");
       }
     }
   }
 }
Exemplo n.º 4
0
 /**
  * Construct a JSONArray from a JSONTokener.
  *
  * @param x A JSONTokener
  * @throws JSONException If there is a syntax error.
  * @nowebref
  */
 protected JSONArray(JSONTokener x) {
   this();
   if (x.nextClean() != '[') {
     throw new RuntimeException("A JSONArray text must start with '['");
   }
   if (x.nextClean() != ']') {
     x.back();
     for (; ; ) {
       if (x.nextClean() == ',') {
         x.back();
         myArrayList.add(JSONObject.NULL);
       } else {
         x.back();
         myArrayList.add(x.nextValue());
       }
       switch (x.nextClean()) {
         case ';':
         case ',':
           if (x.nextClean() == ']') {
             return;
           }
           x.back();
           break;
         case ']':
           return;
         default:
           throw new RuntimeException("Expected a ',' or ']'");
       }
     }
   }
 }
Exemplo n.º 5
0
 private JSONArray(JSONTokener x) throws JSONException {
   this.myArrayList = new ArrayList<Object>();
   if (x.nextClean() != '[') {
     throw x.syntaxError("A JSONArray text must start with '['");
   }
   if (x.nextClean() != ']') {
     x.back();
     for (; ; ) {
       if (x.nextClean() == ',') {
         x.back();
         this.myArrayList.add(null);
       } else {
         x.back();
         this.myArrayList.add(x.nextValue());
       }
       switch (x.nextClean()) {
         case ';':
         case ',':
           if (x.nextClean() == ']') {
             return;
           }
           x.back();
           break;
         case ']':
           return;
         default:
           throw x.syntaxError("Expected a ',' or ']'");
       }
     }
   }
 }
Exemplo n.º 6
0
 /**
  * Construct a JSONArray from a JSONTokener.
  *
  * @param x A JSONTokener
  * @throws JSONException If there is a syntax error.
  */
 @SuppressWarnings("unchecked")
 public JSONArray(JSONTokener x) throws JSONException {
   this();
   char c = x.nextClean();
   char q;
   if (c == '[') {
     q = ']';
   } else if (c == '(') {
     q = ')';
   } else {
     throw x.syntaxError("A JSONArray text must start with '['");
   }
   if (x.nextClean() == ']') {
     return;
   }
   x.back();
   for (; ; ) {
     if (x.nextClean() == ',') {
       x.back();
       this.myArrayList.add(null);
     } else {
       x.back();
       this.myArrayList.add(x.nextValue());
     }
     c = x.nextClean();
     switch (c) {
       case ';':
       case ',':
         if (x.nextClean() == ']') {
           return;
         }
         x.back();
         break;
       case ']':
       case ')':
         if (q != c) {
           throw x.syntaxError("Expected a '" + new Character(q) + "'");
         }
         return;
       default:
         throw x.syntaxError("Expected a ',' or ']'");
     }
   }
 }
Exemplo n.º 7
0
  private void parse(JSONTokener tokenizer) {
    if (tokenizer.nextClean() != '[') {
      throw tokenizer.syntaxError("A JSONArray text must start with '['");
    }

    if (tokenizer.nextClean() == ']') {
      return;
    }

    tokenizer.back();

    while (true) {
      if (tokenizer.nextClean() == ',') {
        tokenizer.back();
        list.add(JSONObject.NULL);
      } else {
        tokenizer.back();
        list.add(tokenizer.nextValue());
      }

      switch (tokenizer.nextClean()) {
        case ';':
        case ',':
          if (tokenizer.nextClean() == ']') {
            return;
          }
          tokenizer.back();
          break;

        case ']':
          return;

        default:
          throw tokenizer.syntaxError("Expected a ',' or ']'");
      }
    }
  }
Exemplo n.º 8
0
    private JSONObject(JSONTokener x) throws JSONException {
      this.map = new HashMap<String, Object>();
      char c;
      String key;

      if (x.nextClean() != '{') {
        throw x.syntaxError("A JSONObject text must begin with '{'");
      }
      for (; ; ) {
        c = x.nextClean();
        switch (c) {
          case 0:
            throw x.syntaxError("A JSONObject text must end with '}'");
          case '}':
            return;
          default:
            x.back();
            key = x.nextValue().toString();
        }

        // The key is followed by ':'. We will also tolerate '=' or '=>'.

        c = x.nextClean();
        if (c == '=') {
          if (x.next() != '>') {
            x.back();
          }
        } else if (c != ':') {
          throw x.syntaxError("Expected a ':' after a key");
        }
        if (key == null) throw new JSONException("Null key.");

        Object value = x.nextValue();

        if (value != null) {
          if (this.has(key)) {
            throw new JSONException("Duplicate key \"" + key + "\"");
          }
          if (value instanceof Double) {
            if (((Double) value).isInfinite() || ((Double) value).isNaN()) {
              throw new JSONException("JSON does not allow non-finite numbers.");
            }
          } else if (value instanceof Float) {
            if (((Float) value).isInfinite() || ((Float) value).isNaN()) {
              throw new JSONException("JSON does not allow non-finite numbers.");
            }
          }
          this.map.put(key, value);
        }

        // Pairs are separated by ','. We will also tolerate ';'.

        switch (x.nextClean()) {
          case ';':
          case ',':
            if (x.nextClean() == '}') {
              return;
            }
            x.back();
            break;
          case '}':
            return;
          default:
            throw x.syntaxError("Expected a ',' or '}'");
        }
      }
    }