/**
  * Convert a cookie specification string into a JSONObject. The string will contain a name value
  * pair separated by '='. The name and the value will be unescaped, possibly converting '+' and
  * '%' sequences. The cookie properties may follow, separated by ';', also represented as
  * name=value (except the secure property, which does not have a value). The name will be stored
  * under the key "name", and the value will be stored under the key "value". This method does not
  * do checking or validation of the parameters. It only converts the cookie string into a
  * JSONObject.
  *
  * @param string The cookie specification string.
  * @return A JSONObject containing "name", "value", and possibly other members.
  * @throws JSONException
  */
 public static JSONObject toJSONObject(String string) throws JSONException {
   String name;
   JSONObject jo = new JSONObject();
   Object value;
   JSONTokener x = new JSONTokener(string);
   jo.put("name", x.nextTo('='));
   x.next('=');
   jo.put("value", x.nextTo(';'));
   x.next();
   while (x.more()) {
     name = unescape(x.nextTo("=;"));
     if (x.next() != '=') {
       if (name.equals("secure")) {
         value = Boolean.TRUE;
       } else {
         throw x.syntaxError("Missing '=' in cookie parameter.");
       }
     } else {
       value = unescape(x.nextTo(';'));
       x.next();
     }
     jo.put(name, value);
   }
   return jo;
 }
Пример #2
0
 /**
  * Produce a JSONArray of strings from a row of comma delimited values.
  * @param x A JSONTokener of the source text.
  * @return A JSONArray of strings.
  * @throws JSONException
  */
 public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
     JSONArray ja = new JSONArray();
     for (;;) {
         String value = getValue(x);
         char c = x.next();
         if (value == null || 
         		(ja.length() == 0 && value.length() == 0 && c != ',')) {
             return null;
         }
         ja.put(value);
         for (;;) {                
             if (c == ',') {
                 break;
             }
             if (c != ' ') {
                 if (c == '\n' || c == '\r' || c == 0) {
                     return ja;
                 }
                 throw x.syntaxError("Bad character '" + c + "' (" +
                         (int)c + ").");
             }
             c = x.next();
         }
     }
 }
Пример #3
0
 /**
  * Get the next value. The value can be wrapped in quotes. The value can
  * be empty.
  * @param x A JSONTokener of the source text.
  * @return The value string, or null if empty.
  * @throws JSONException if the quoted string is badly formed.
  */
 private static String getValue(JSONTokener x) throws JSONException {
     char c;
     char q;
     StringBuffer sb;
     do {
         c = x.next();
     } while (c == ' ' || c == '\t');
     switch (c) {
     case 0:
         return null;
     case '"':
     case '\'':
     	q = c;
     	sb = new StringBuffer();
     	for (;;) {
     		c = x.next();
     		if (c == q) {
     			break;
     		}
             if (c == 0 || c == '\n' || c == '\r') {
                 throw x.syntaxError("Missing close quote '" + q + "'.");
             }
             sb.append(c);
     	}
         return sb.toString();
     case ',':
         x.back();
         return "";
     default:
         x.back();
         return x.nextTo(',');
     }
 }
Пример #4
0
 /**
  * Convert a cookie list into a JSONObject. A cookie list is a sequence of name/value pairs. The
  * names are separated from the values by '='. The pairs are separated by ';'. The names and the
  * values will be unescaped, possibly converting '+' and '%' sequences.
  *
  * <p>To add a cookie to a cooklist, cookielistJSONObject.put(cookieJSONObject.getString("name"),
  * cookieJSONObject.getString("value"));
  *
  * @param string A cookie list string
  * @return A JSONObject
  * @throws JSONException
  */
 public static JSONObject toJSONObject(String string) throws JSONException {
   JSONObject jo = new JSONObject();
   JSONTokener x = new JSONTokener(string);
   while (x.more()) {
     String name = Cookie.unescape(x.nextTo('='));
     x.next('=');
     jo.put(name, Cookie.unescape(x.nextTo(';')));
     x.next();
   }
   return jo;
 }
Пример #5
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 '}'");
      }
    }
  }
Пример #6
0
 /**
  * Get the next value. The value can be wrapped in quotes. The value can be empty.
  *
  * @param x A JSONTokener of the source text.
  * @return The value string, or null if empty.
  * @throws JSONException if the quoted string is badly formed.
  */
 private static String getValue(JSONTokener x) throws JSONException {
   char c;
   do {
     c = x.next();
   } while (c <= ' ' && c != 0);
   switch (c) {
     case 0:
       return null;
     case '"':
     case '\'':
       return x.nextString(c);
     case ',':
       x.back();
       return "";
     default:
       x.back();
       return x.nextTo(',');
   }
 }
Пример #7
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 '}'");
        }
      }
    }