add methods

This commit is contained in:
Looly
2024-10-02 14:57:15 +08:00
parent 3e79778272
commit 463a8308e0
7 changed files with 245 additions and 9 deletions

View File

@@ -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"})

View File

@@ -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;