Exemple #1
0
 /**
  * Maps {@code name} to {@code value}, clobbering any existing name/value mapping with the same
  * name. If the value is {@code null}, any existing mapping for {@code name} is removed.
  *
  * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double,
  *     {@link #NULL}, or {@code null}. May not be {@link Double#isNaN() NaNs} or {@link
  *     Double#isInfinite() infinities}.
  * @return this object.
  */
 public JSONObject put(String name, Object value) throws JSONException {
   if (value == null) {
     nameValuePairs.remove(name);
     return this;
   }
   if (value instanceof Number) {
     // deviate from the original by checking all Numbers, not just floats & doubles
     JSON.checkDouble(((Number) value).doubleValue());
   }
   nameValuePairs.put(checkName(name), value);
   return this;
 }
Exemple #2
0
  /**
   * Encodes the number as a JSON string.
   *
   * @param number a finite value. May not be {@link Double#isNaN() NaNs} or {@link
   *     Double#isInfinite() infinities}.
   */
  public static String numberToString(Number number) throws JSONException {
    if (number == null) {
      throw new JSONException("Number must be non-null");
    }

    double doubleValue = number.doubleValue();
    JSON.checkDouble(doubleValue);

    // the original returns "-0" instead of "-0.0" for negative zero
    if (number.equals(NEGATIVE_ZERO)) {
      return "-0";
    }

    long longValue = number.longValue();
    if (doubleValue == (double) longValue) {
      return Long.toString(longValue);
    }

    return number.toString();
  }
Exemple #3
0
  /**
   * Appends {@code value} to the array already mapped to {@code name}. If this object has no
   * mapping for {@code name}, this inserts a new mapping. If the mapping exists but its value is
   * not an array, the existing and new values are inserted in order into a new array which is
   * itself mapped to {@code name}. In aggregate, this allows values to be added to a mapping one at
   * a time.
   *
   * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double,
   *     {@link #NULL} or null. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
   *     infinities}.
   */
  public JSONObject accumulate(String name, Object value) throws JSONException {
    Object current = nameValuePairs.get(checkName(name));
    if (current == null) {
      return put(name, value);
    }

    // check in accumulate, since array.put(Object) doesn't do any checking
    if (value instanceof Number) {
      JSON.checkDouble(((Number) value).doubleValue());
    }

    if (current instanceof JSONArray) {
      JSONArray array = (JSONArray) current;
      array.put(value);
    } else {
      JSONArray array = new JSONArray();
      array.put(current);
      array.put(value);
      nameValuePairs.put(name, array);
    }
    return this;
  }
Exemple #4
0
 /**
  * Maps {@code name} to {@code value}, clobbering any existing name/value mapping with the same
  * name.
  *
  * @param value a finite value. May not be {@link Double#isNaN() NaNs} or {@link
  *     Double#isInfinite() infinities}.
  * @return this object.
  */
 public JSONObject put(String name, double value) throws JSONException {
   nameValuePairs.put(checkName(name), JSON.checkDouble(value));
   return this;
 }