org.codehaus.jettison.json.JSONObject.testValidity()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(85)

本文整理了Java中org.codehaus.jettison.json.JSONObject.testValidity()方法的一些代码示例,展示了JSONObject.testValidity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.testValidity()方法的具体详情如下:
包路径:org.codehaus.jettison.json.JSONObject
类名称:JSONObject
方法名:testValidity

JSONObject.testValidity介绍

[英]Throw an exception if the object is an NaN or infinite number.
[中]如果对象是NaN或无限数,则引发异常。

代码示例

代码示例来源:origin: org.codehaus.jettison/jettison

/**
 * Append a double value. This increases the array's length by one.
 *
 * @param value A double value.
 * @throws JSONException if the value is not finite.
 * @return this.
 */
public JSONArray put(double value) throws JSONException {
  Double d = new Double(value);
  JSONObject.testValidity(d);
  put(d);
  return this;
}

代码示例来源:origin: org.codehaus.jettison/jettison

/**
   * Produce a string from a Number.
   * @param  n A Number
   * @return A String.
   * @throws JSONException If n is a non-finite number.
   */
  static public String numberToString(Number n)
      throws JSONException {
    if (n == null) {
      throw new JSONException("Null pointer");
    }
    testValidity(n);

// Shave off trailing zeros and decimal point, if possible.

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
      while (s.endsWith("0")) {
        s = s.substring(0, s.length() - 1);
      }
      if (s.endsWith(".")) {
        s = s.substring(0, s.length() - 1);
      }
    }
    return s;
  }

代码示例来源:origin: org.codehaus.jettison/jettison

testValidity(value);
if (!checkExistingValue || !this.myHashMap.containsKey(key)) {
  this.myHashMap.put(key, value);

代码示例来源:origin: org.codehaus.jettison/jettison

/**
 * Put or replace an object value in the JSONArray. If the index is greater
 *  than the length of the JSONArray, then null elements will be added as
 *  necessary to pad it out.
 * @param index The subscript.
 * @param value The value to put into the array. The value should be a
 *  Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
 *  JSONObject.NULL object.
 * @return this.
 * @throws JSONException If the index is negative or if the the value is
 *  an invalid number.
 */
public JSONArray put(int index, Object value) throws JSONException {
  JSONObject.testValidity(value);
  if (index < 0) {
    throw new JSONException("JSONArray[" + index + "] not found.");
  }
  if (index < length()) {
    this.myArrayList.set(index, value);
  } else {
    while (index != length()) {
      put(JSONObject.NULL);
    }
    put(value);
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/jettison

/**
 * Append values to the array under a key. If the key does not exist in the
 * JSONObject, then the key is put in the JSONObject with its value being a
 * JSONArray containing the value parameter. If the key was already
 * associated with a JSONArray, then the value parameter is appended to it.
 * @param key   A key string.
 * @param value An object to be accumulated under the key.
 * @return this.
 * @throws JSONException If the key is null or if the current value 
 *  associated with the key is not a JSONArray.
 */
public JSONObject append(String key, Object value)
    throws JSONException {
  testValidity(value);
  Object o = opt(key);
  if (o == null) {
    put(key, new JSONArray().put(value));
  } else if (!(o instanceof JSONArray)){
    throw new JSONException("JSONObject[" + key + 
          "] is not a JSONArray.");
  } else {
    ((JSONArray)o).put(value);
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/jettison

/**
 * Accumulate values under a key. It is similar to the put method except
 * that if there is already an object stored under the key then a
 * JSONArray is stored under the key to hold all of the accumulated values.
 * If there is already a JSONArray, then the new value is appended to it.
 * In contrast, the put method replaces the previous value.
 * @param key   A key string.
 * @param value An object to be accumulated under the key.
 * @return this.
 * @throws JSONException If the value is an invalid number
 *  or if the key is null.
 */
public JSONObject accumulate(String key, Object value)
    throws JSONException {
  testValidity(value);
  Object o = opt(key);
  if (o == null) {
    put(key, value);
  } else if (o instanceof JSONArray) {
    ((JSONArray)o).put(value);
  } else {
    put(key, new JSONArray().put(o).put(value));
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
 * Append a double value. This increases the array's length by one.
 *
 * @param value A double value.
 * @throws JSONException if the value is not finite.
 * @return this.
 */
public JSONArray put(double value) throws JSONException {
  Double d = new Double(value);
  JSONObject.testValidity(d);
  put(d);
  return this;
}

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
   * Produce a string from a Number.
   * @param  n A Number
   * @return A String.
   * @throws JSONException If n is a non-finite number.
   */
  static public String numberToString(Number n)
      throws JSONException {
    if (n == null) {
      throw new JSONException("Null pointer");
    }
    testValidity(n);

// Shave off trailing zeros and decimal point, if possible.

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
      while (s.endsWith("0")) {
        s = s.substring(0, s.length() - 1);
      }
      if (s.endsWith(".")) {
        s = s.substring(0, s.length() - 1);
      }
    }
    return s;
  }

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
 * Put a key/value pair in the JSONObject. If the value is null,
 * then the key will be removed from the JSONObject if it is present.
 * @param key   A key string.
 * @param value An object which is the value. It should be of one of these
 *  types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
 *  or the JSONObject.NULL object.
 * @return this.
 * @throws JSONException If the value is non-finite number
 *  or if the key is null.
 */
public JSONObject put(String key, Object value) throws JSONException {
  if (key == null) {
    throw new JSONException("Null key.");
  }
  if (value != null) {
    testValidity(value);
    this.myHashMap.put(key, value);
  } else {
    remove(key);
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
 * Put or replace an object value in the JSONArray. If the index is greater
 *  than the length of the JSONArray, then null elements will be added as
 *  necessary to pad it out.
 * @param index The subscript.
 * @param value The value to put into the array. The value should be a
 *  Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
 *  JSONObject.NULL object.
 * @return this.
 * @throws JSONException If the index is negative or if the the value is
 *  an invalid number.
 */
public JSONArray put(int index, Object value) throws JSONException {
  JSONObject.testValidity(value);
  if (index < 0) {
    throw new JSONException("JSONArray[" + index + "] not found.");
  }
  if (index < length()) {
    this.myArrayList.set(index, value);
  } else {
    while (index != length()) {
      put(JSONObject.NULL);
    }
    put(value);
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
 * Accumulate values under a key. It is similar to the put method except
 * that if there is already an object stored under the key then a
 * JSONArray is stored under the key to hold all of the accumulated values.
 * If there is already a JSONArray, then the new value is appended to it.
 * In contrast, the put method replaces the previous value.
 * @param key   A key string.
 * @param value An object to be accumulated under the key.
 * @return this.
 * @throws JSONException If the value is an invalid number
 *  or if the key is null.
 */
public JSONObject accumulate(String key, Object value)
    throws JSONException {
  testValidity(value);
  Object o = opt(key);
  if (o == null) {
    put(key, value);
  } else if (o instanceof JSONArray) {
    ((JSONArray)o).put(value);
  } else {
    put(key, new JSONArray().put(o).put(value));
  }
  return this;
}

代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison

/**
 * Append values to the array under a key. If the key does not exist in the
 * JSONObject, then the key is put in the JSONObject with its value being a
 * JSONArray containing the value parameter. If the key was already
 * associated with a JSONArray, then the value parameter is appended to it.
 * @param key   A key string.
 * @param value An object to be accumulated under the key.
 * @return this.
 * @throws JSONException If the key is null or if the current value 
 *  associated with the key is not a JSONArray.
 */
public JSONObject append(String key, Object value)
    throws JSONException {
  testValidity(value);
  Object o = opt(key);
  if (o == null) {
    put(key, new JSONArray().put(value));
  } else if (!(o instanceof JSONArray)){
    throw new JSONException("JSONObject[" + key + 
          "] is not a JSONArray.");
  } else {
    put(key, new JSONArray().put(o).put(value));
  }
  return this;
}

相关文章