remove methods

This commit is contained in:
Looly 2022-04-28 03:14:54 +08:00
parent b21f650474
commit 17cd183722
7 changed files with 71 additions and 189 deletions

View File

@ -107,25 +107,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @throws JSONException 非数组或集合
*/
public JSONArray(Object object) throws JSONException {
this(object, true);
}
/**
* 从对象构造<br>
* 支持以下类型的参数
*
* <pre>
* 1. 数组
* 2. {@link Iterable}对象
* 3. JSON数组字符串
* </pre>
*
* @param object 数组或集合或JSON数组字符串
* @param ignoreNullValue 是否忽略空值
* @throws JSONException 非数组或集合
*/
public JSONArray(Object object, boolean ignoreNullValue) throws JSONException {
this(object, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
this(object, JSONConfig.create());
}
/**
@ -368,13 +350,12 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
return rawList.remove(o);
}
@SuppressWarnings("NullableProblems")
@SuppressWarnings({"NullableProblems", "SlowListContainsAll"})
@Override
public boolean containsAll(Collection<?> c) {
return rawList.containsAll(c);
}
@SuppressWarnings("NullableProblems")
@Override
public boolean addAll(Collection<?> c) {
if (CollUtil.isEmpty(c)) {
@ -386,7 +367,6 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
return true;
}
@SuppressWarnings("NullableProblems")
@Override
public boolean addAll(int index, Collection<?> c) {
if (CollUtil.isEmpty(c)) {
@ -399,13 +379,11 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
return rawList.addAll(index, list);
}
@SuppressWarnings("NullableProblems")
@Override
public boolean removeAll(Collection<?> c) {
return this.rawList.removeAll(c);
}
@SuppressWarnings("NullableProblems")
@Override
public boolean retainAll(Collection<?> c) {
return this.rawList.retainAll(c);

View File

@ -4,12 +4,9 @@ import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.map.MapWrapper;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.serialize.JSONWriter;
import java.io.StringWriter;
@ -46,20 +43,10 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
// -------------------------------------------------------------------------------------------------------------------- Constructor start
/**
* 构造初始容量为 {@link #DEFAULT_CAPACITY}KEY
* 构造初始容量为 {@link #DEFAULT_CAPACITY}KEY
*/
public JSONObject() {
this(DEFAULT_CAPACITY, false);
}
/**
* 构造初始容量为 {@link #DEFAULT_CAPACITY}
*
* @param isOrder 是否有序
* @since 3.0.9
*/
public JSONObject(boolean isOrder) {
this(DEFAULT_CAPACITY, isOrder);
this(JSONConfig.create());
}
/**
@ -97,48 +84,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param source JavaBean或者Map对象或者String
*/
public JSONObject(Object source) {
this(source, InternalJSONUtil.defaultIgnoreNullValue(source));
}
/**
* 构建JSONObject规则如下
* <ol>
* <li>value为Map将键值对加入JSON对象</li>
* <li>value为JSON字符串CharSequence使用JSONTokener解析</li>
* <li>value为JSONTokener直接解析</li>
* <li>value为普通JavaBean如果为普通的JavaBean调用其getters方法getXXX或者isXXX获得值加入到JSON对象例如如果JavaBean对象中有个方法getName()值为"张三"获得的键值对为name: "张三"</li>
* </ol>
*
* @param source JavaBean或者Map对象或者String
* @param ignoreNullValue 是否忽略空值
* @since 3.0.9
*/
public JSONObject(Object source, boolean ignoreNullValue) {
this(source, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
}
/**
* 构建JSONObject规则如下
* <ol>
* <li>value为Map将键值对加入JSON对象</li>
* <li>value为JSON字符串CharSequence使用JSONTokener解析</li>
* <li>value为JSONTokener直接解析</li>
* <li>value为普通JavaBean如果为普通的JavaBean调用其getters方法getXXX或者isXXX获得值加入到JSON对象例如如果JavaBean对象中有个方法getName()值为"张三"获得的键值对为name: "张三"</li>
* </ol>
*
* @param source JavaBean或者Map对象或者String
* @param ignoreNullValue 是否忽略空值如果source为JSON字符串不忽略空值
* @param isOrder 是否有序
* @since 4.2.2
* @deprecated isOrder参数不再需要JSONObject默认有序
*/
@SuppressWarnings("unused")
@Deprecated
public JSONObject(Object source, boolean ignoreNullValue, boolean isOrder) {
this(source, JSONConfig.create()//
.setIgnoreCase((source instanceof CaseInsensitiveMap))//
.setIgnoreNullValue(ignoreNullValue)
);
this(source, JSONConfig.create().setIgnoreNullValue(InternalJSONUtil.defaultIgnoreNullValue(source)));
}
/**
@ -184,60 +130,6 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
this(DEFAULT_CAPACITY, config);
ObjectMapper.of(source).map(this, filter);
}
/**
* 构建指定name列表对应的键值对为新的JSONObject情况如下
*
* <pre>
* 1. 若obj为Map则获取name列表对应键值对
* 2. 若obj为普通Bean使用反射方式获取字段名和字段值
* </pre>
* <p>
* KEY或VALUE任意一个为null则不加入字段不存在也不加入<br>
* 若names列表为空则字段全部加入
*
* @param source 包含需要字段的Bean对象或者Map对象
* @param names 需要构建JSONObject的字段名列表
*/
public JSONObject(Object source, String... names) {
this();
if (ArrayUtil.isEmpty(names)) {
ObjectMapper.of(source).map(this, null);
return;
}
if (source instanceof Map) {
Object value;
for (String name : names) {
value = ((Map<?, ?>) source).get(name);
this.putOnce(name, value);
}
} else {
for (String name : names) {
try {
this.putOpt(name, ReflectUtil.getFieldValue(source, name));
} catch (Exception ignore) {
// ignore
}
}
}
}
/**
* 从JSON字符串解析为JSON对象对于排序单独配置参数
*
* @param source 以大括号 {} 包围的字符串其中KEY和VALUE使用 : 分隔每个键值对使用逗号分隔
* @param isOrder 是否有序
* @throws JSONException JSON字符串语法错误
* @since 4.2.2
* @deprecated isOrder无效
*/
@SuppressWarnings("unused")
@Deprecated
public JSONObject(CharSequence source, boolean isOrder) throws JSONException {
this(source, JSONConfig.create());
}
// -------------------------------------------------------------------------------------------------------------------- Constructor end
@Override
@ -305,6 +197,19 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
BeanPath.create(expression).set(this, value);
}
/**
* PUT 键值对到JSONObject中在忽略null模式下如果值为{@code null}将此键移除
*
* @param key
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
*/
@Override
public Object put(String key, Object value) throws JSONException {
return put(key, value, null, false);
}
/**
* 设置键值对到JSONObject中在忽略null模式下如果值为{@code null}将此键移除
*
@ -329,34 +234,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @since 5.8.0
*/
public JSONObject set(String key, Object value, Filter<MutablePair<String, Object>> filter, boolean checkDuplicate) throws JSONException {
if (null == key) {
return this;
}
// 添加前置过滤通过MutablePair实现过滤修改键值对等
if (null != filter) {
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
if (filter.accept(pair)) {
// 使用修改后的键值对
key = pair.getKey();
value = pair.getValue();
} else {
// 键值对被过滤
return this;
}
}
final boolean ignoreNullValue = this.config.isIgnoreNullValue();
if (ObjectUtil.isNull(value) && ignoreNullValue) {
// 忽略值模式下如果值为空清除key
this.remove(key);
} else {
if (checkDuplicate && containsKey(key)) {
throw new JSONException("Duplicate key \"{}\"", key);
}
super.put(key, JSONUtil.wrap(InternalJSONUtil.testValidity(value), this.config));
}
put(key, value, filter, checkDuplicate);
return this;
}
@ -549,4 +427,43 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
clone.config = this.config;
return clone;
}
/**
* 设置键值对到JSONObject中在忽略null模式下如果值为{@code null}将此键移除
*
* @param key
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @param filter 键值对过滤编辑器可以通过实现此接口完成解析前对键值对的过滤和修改操作{@code null}表示不过滤
* @param checkDuplicate 是否检查重复键如果为{@code true}则出现重复键时抛出{@link JSONException}异常
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
* @since 5.8.0
*/
private Object put(String key, Object value, Filter<MutablePair<String, Object>> filter, boolean checkDuplicate) throws JSONException {
if (null == key) {
return this;
}
// 添加前置过滤通过MutablePair实现过滤修改键值对等
if (null != filter) {
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
if (filter.accept(pair)) {
// 使用修改后的键值对
key = pair.getKey();
value = pair.getValue();
} else {
// 键值对被过滤
return this;
}
}
final boolean ignoreNullValue = this.config.isIgnoreNullValue();
if (ObjectUtil.isNull(value) && ignoreNullValue) {
// 忽略值模式下如果值为空清除key
return this.remove(key);
} else if (checkDuplicate && containsKey(key)) {
throw new JSONException("Duplicate key \"{}\"", key);
}
return super.put(key, JSONUtil.wrap(InternalJSONUtil.testValidity(value), this.config));
}
}

View File

@ -79,16 +79,6 @@ public class JSONUtil {
return new JSONArray(config);
}
/**
* JSON字符串转JSONObject对象
*
* @param jsonStr JSON字符串
* @return JSONObject
*/
public static JSONObject parseObj(String jsonStr) {
return new JSONObject(jsonStr);
}
/**
* JSON字符串转JSONObject对象<br>
* 此方法会忽略空值但是对JSON字符串不影响
@ -97,7 +87,7 @@ public class JSONUtil {
* @return JSONObject
*/
public static JSONObject parseObj(Object obj) {
return parseObj(obj, null);
return new JSONObject(obj);
}
/**
@ -110,7 +100,7 @@ public class JSONUtil {
* @since 5.3.1
*/
public static JSONObject parseObj(Object obj, JSONConfig config) {
return new JSONObject(obj, ObjectUtil.defaultIfNull(config, JSONConfig::create));
return new JSONObject(obj, config);
}
/**
@ -122,17 +112,7 @@ public class JSONUtil {
* @since 3.0.9
*/
public static JSONObject parseObj(Object obj, boolean ignoreNullValue) {
return new JSONObject(obj, ignoreNullValue);
}
/**
* JSON字符串转JSONArray
*
* @param jsonStr JSON字符串
* @return JSONArray
*/
public static JSONArray parseArray(String jsonStr) {
return new JSONArray(jsonStr);
return new JSONObject(obj, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
}
/**
@ -143,7 +123,7 @@ public class JSONUtil {
* @since 3.0.8
*/
public static JSONArray parseArray(Object arrayOrCollection) {
return parseArray(arrayOrCollection, null);
return new JSONArray(arrayOrCollection);
}
/**
@ -167,7 +147,7 @@ public class JSONUtil {
* @since 3.2.3
*/
public static JSONArray parseArray(Object arrayOrCollection, boolean ignoreNullValue) {
return new JSONArray(arrayOrCollection, ignoreNullValue);
return new JSONArray(arrayOrCollection, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
}
/**

View File

@ -76,7 +76,7 @@ public class JSONArrayTest {
Assert.assertFalse(jsonArray.getJSONObject(1).containsKey("result"));
// 不忽略null则null的键值对被保留
jsonArray = new JSONArray(jsonStr, false);
jsonArray = JSONUtil.parseArray(jsonStr, false);
Assert.assertTrue(jsonArray.getJSONObject(1).containsKey("result"));
}

View File

Can't render this file because it contains an unexpected character in line 2 and column 33.

View File

@ -0,0 +1,7 @@
# 这是一行注释,读取时应忽略
a,b,c,d
1,2,3,4
# 这是一行注释,读取时应忽略
q,w,e,r,"我是一段
带换行的内容"
a,s,d,f
Can't render this file because it has a wrong number of fields in line 2.