mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add methods
This commit is contained in:
@@ -112,11 +112,69 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
return this.raw.get(key);
|
||||
}
|
||||
|
||||
// region ----- addValue
|
||||
/**
|
||||
* 加入{@code null}元素,如果设置中忽略null值,则忽略
|
||||
*
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addNull() {
|
||||
this.add(null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append an object value. This increases the array's length by one. <br>
|
||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||
*
|
||||
* @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。
|
||||
* @param value Number值
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addValue(final Boolean value) {
|
||||
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||
this.add(this.factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||
*
|
||||
* @param value Number值
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addValue(final Number value) {
|
||||
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||
this.add(this.factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||
*
|
||||
* @param value Character值
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addValue(final Character value) {
|
||||
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||
this.add(this.factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||
*
|
||||
* @param value String值
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addValue(final String value) {
|
||||
// add时,不解析字符串,而是作为JSONPrimitive对待
|
||||
this.add(this.factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||
*
|
||||
* @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the {@code null}。
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray addValue(final Object value) {
|
||||
@@ -124,6 +182,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
this.add(this.factory.getMapper().toJSON(value, false));
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 根据给定名列表,与其位置对应的值组成JSONObject
|
||||
@@ -204,6 +263,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
return this.raw.set(index, element);
|
||||
}
|
||||
|
||||
// region ----- add
|
||||
@Override
|
||||
public boolean add(final JSON element) {
|
||||
if (null == element && config().isIgnoreNullValue()) {
|
||||
@@ -235,8 +295,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
}
|
||||
this.add(element);
|
||||
}
|
||||
|
||||
}
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({"unchecked"})
|
||||
|
@@ -227,6 +227,70 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置设置{@code null}值。在忽略null模式下移除对应键值对。
|
||||
*
|
||||
* @param key 键
|
||||
* @return this.
|
||||
*/
|
||||
public JSONObject putNull(final String key){
|
||||
this.put(key, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value Boolean类型对象
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject putValue(final String key, final Boolean value) throws JSONException {
|
||||
this.put(key, factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value Number类型对象
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject putValue(final String key, final Number value) throws JSONException {
|
||||
this.put(key, factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value Character类型对象
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject putValue(final String key, final Character value) throws JSONException {
|
||||
this.put(key, factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value String类型对象
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject putValue(final String key, final String value) throws JSONException {
|
||||
// put时,不解析字符串,而是作为JSONPrimitive对待
|
||||
this.put(key, factory.ofPrimitive(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
@@ -236,6 +300,9 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject putValue(final String key, final Object value) throws JSONException {
|
||||
if(null == value){
|
||||
return putNull(key);
|
||||
}
|
||||
// put时,如果value为字符串,不解析,而是作为JSONPrimitive对待
|
||||
this.put(key, factory.getMapper().toJSON(value, false));
|
||||
return this;
|
||||
|
Reference in New Issue
Block a user