mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -2,15 +2,14 @@ package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
|
||||
import cn.hutool.core.map.CaseInsensitiveTreeMap;
|
||||
import cn.hutool.core.math.NumberUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.math.NumberUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
@@ -18,6 +17,7 @@ import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 内部JSON工具类,仅用于JSON内部使用
|
||||
@@ -138,9 +138,10 @@ public final class InternalJSONUtil {
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param predicate 属性过滤器,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return JSONObject
|
||||
*/
|
||||
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Filter<MutableEntry<String, Object>> filter) {
|
||||
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||
final int last = path.length - 1;
|
||||
JSONObject target = jsonObject;
|
||||
@@ -149,11 +150,11 @@ public final class InternalJSONUtil {
|
||||
JSONObject nextTarget = target.getJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject(target.getConfig());
|
||||
target.setOnce(segment, nextTarget, filter);
|
||||
target.setOnce(segment, nextTarget, predicate);
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.setOnce(path[last], value, filter);
|
||||
target.setOnce(path[last], value, predicate);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,6 @@ package cn.hutool.json;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.impl.ArrayConverter;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.lang.mutable.MutableObj;
|
||||
@@ -20,6 +19,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON数组<br>
|
||||
@@ -142,13 +142,13 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
*
|
||||
* @param object 数组或集合或JSON数组字符串
|
||||
* @param jsonConfig JSON选项
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @throws JSONException 非数组或集合
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONArray(final Object object, final JSONConfig jsonConfig, final Filter<Mutable<Object>> filter) throws JSONException {
|
||||
public JSONArray(final Object object, final JSONConfig jsonConfig, final Predicate<Mutable<Object>> predicate) throws JSONException {
|
||||
this(DEFAULT_CAPACITY, jsonConfig);
|
||||
ObjectMapper.of(object).map(this, filter);
|
||||
ObjectMapper.of(object).map(this, predicate);
|
||||
}
|
||||
// endregion
|
||||
|
||||
@@ -365,7 +365,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
}
|
||||
final ArrayList<Object> list = new ArrayList<>(c.size());
|
||||
for (final Object object : c) {
|
||||
if(null == object && config.isIgnoreNullValue()){
|
||||
if (null == object && config.isIgnoreNullValue()) {
|
||||
continue;
|
||||
}
|
||||
this.add(index);
|
||||
@@ -407,15 +407,15 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
*
|
||||
* @param index 位置
|
||||
* @param element 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param filter 过滤器,可以修改值,key(index)无法修改
|
||||
* @param filter 过滤器,可以修改值,key(index)无法修改,{@link Predicate#test(Object)}为{@code true}保留,null表示全部保留。
|
||||
* @return 替换的值,即之前的值
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public Object set(final int index, Object element, final Filter<MutableEntry<Integer, Object>> filter) {
|
||||
public Object set(final int index, Object element, final Predicate<MutableEntry<Integer, Object>> filter) {
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != filter) {
|
||||
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, element);
|
||||
if (filter.accept(pair)) {
|
||||
if (filter.test(pair)) {
|
||||
// 使用修改后的值
|
||||
element = pair.getValue();
|
||||
}
|
||||
@@ -426,7 +426,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
add(index, element);
|
||||
return null;
|
||||
}
|
||||
if(null == element && config.isIgnoreNullValue()){
|
||||
if (null == element && config.isIgnoreNullValue()) {
|
||||
return null;
|
||||
}
|
||||
return this.rawList.set(index, JSONUtil.wrap(element, this.config));
|
||||
@@ -434,7 +434,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public void add(int index, final Object element) {
|
||||
if(null == element && config.isIgnoreNullValue()){
|
||||
if (null == element && config.isIgnoreNullValue()) {
|
||||
return;
|
||||
}
|
||||
if (index < this.size()) {
|
||||
@@ -444,7 +444,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
InternalJSONUtil.testValidity(element);
|
||||
this.rawList.add(index, JSONUtil.wrap(element, this.config));
|
||||
} else {
|
||||
if(false == config.isIgnoreNullValue()){
|
||||
if (false == config.isIgnoreNullValue()) {
|
||||
while (index != this.size()) {
|
||||
// 非末尾,则填充null
|
||||
this.add(null);
|
||||
@@ -517,14 +517,14 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* 支持过滤器,即选择哪些字段或值不写出
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @param filter 过滤器,可以修改值,key(index)无法修改
|
||||
* @param predicate 过滤器,可以修改值,key(index)无法修改,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return JSON字符串
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public String toJSONString(final int indentFactor, final Filter<MutableEntry<Integer, Object>> filter) {
|
||||
public String toJSONString(final int indentFactor, final Predicate<MutableEntry<Integer, Object>> predicate) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0, filter).toString();
|
||||
return this.write(sw, indentFactor, 0, predicate).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,18 +540,18 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* @param writer writer
|
||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
||||
* @param indent 本级别缩进量
|
||||
* @param filter 过滤器,可以修改值,key(index)无法修改
|
||||
* @param predicate 过滤器,可以修改值,key(index)无法修改,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<Integer, Object>> filter) throws JSONException {
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Predicate<MutableEntry<Integer, Object>> predicate) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
|
||||
.beginArray();
|
||||
|
||||
CollUtil.forEach(this, (value, index) -> {
|
||||
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, value);
|
||||
if (null == filter || filter.accept(pair)) {
|
||||
if (null == predicate || predicate.test(pair)) {
|
||||
jsonWriter.writeValue(pair.getValue());
|
||||
}
|
||||
});
|
||||
@@ -570,24 +570,24 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
/**
|
||||
* 原始添加,添加的对象不做任何处理
|
||||
*
|
||||
* @param obj 添加的对象
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param obj 添加的对象
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return 是否加入成功
|
||||
* @since 5.8.0
|
||||
*/
|
||||
protected boolean addRaw(Object obj, final Filter<Mutable<Object>> filter) {
|
||||
protected boolean addRaw(Object obj, final Predicate<Mutable<Object>> predicate) {
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != filter) {
|
||||
if (null != predicate) {
|
||||
final Mutable<Object> mutable = new MutableObj<>(obj);
|
||||
if (filter.accept(mutable)) {
|
||||
if (predicate.test(mutable)) {
|
||||
// 使用修改后的值
|
||||
obj = mutable.get();
|
||||
}else{
|
||||
} else {
|
||||
// 键值对被过滤
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(null == obj && config.isIgnoreNullValue()){
|
||||
if (null == obj && config.isIgnoreNullValue()) {
|
||||
// 忽略空则不添加
|
||||
return false;
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.MapWrapper;
|
||||
@@ -15,6 +14,7 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON对象<br>
|
||||
@@ -120,14 +120,14 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* 如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象<br>
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
* @param config JSON配置文件,{@code null}则使用默认配置
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
* @param config JSON配置文件,{@code null}则使用默认配置
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject(final Object source, final JSONConfig config, final Filter<MutableEntry<String, Object>> filter) {
|
||||
public JSONObject(final Object source, final JSONConfig config, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
this(DEFAULT_CAPACITY, config);
|
||||
ObjectMapper.of(source).map(this, filter);
|
||||
ObjectMapper.of(source).map(this, predicate);
|
||||
}
|
||||
// -------------------------------------------------------------------------------------------------------------------- Constructor end
|
||||
|
||||
@@ -215,15 +215,15 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
/**
|
||||
* 一次性Put 键值对,如果key已经存在抛出异常,如果键值中有null值,忽略
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param key 键
|
||||
* @param value 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return this
|
||||
* @throws JSONException 值是无穷数字、键重复抛出异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject setOnce(final String key, final Object value, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
|
||||
put(key, value, filter, true);
|
||||
public JSONObject setOnce(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException {
|
||||
put(key, value, predicate, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -232,14 +232,14 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @param checkDuplicate 是否检查重复键,如果为{@code true},则出现重复键时抛出{@link JSONException}异常
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public JSONObject set(final String key, final Object value, final Filter<MutableEntry<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
put(key, value, filter, checkDuplicate);
|
||||
public JSONObject set(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate, final boolean checkDuplicate) throws JSONException {
|
||||
put(key, value, predicate, checkDuplicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -336,14 +336,14 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* 支持过滤器,即选择哪些字段或值不写出
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @param filter 过滤器,同时可以修改编辑键和值
|
||||
* @param predicate 过滤器,同时可以修改编辑键和值,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return JSON字符串
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public String toJSONString(final int indentFactor, final Filter<MutableEntry<String, Object>> filter) {
|
||||
public String toJSONString(final int indentFactor, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0, filter).toString();
|
||||
return this.write(sw, indentFactor, 0, predicate).toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,18 +359,18 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @param writer writer
|
||||
* @param indentFactor 缩进因子,定义每一级别增加的缩进量
|
||||
* @param indent 本级别缩进量
|
||||
* @param filter 过滤器,同时可以修改编辑键和值
|
||||
* @param predicate 过滤器,同时可以修改编辑键和值
|
||||
* @return Writer
|
||||
* @throws JSONException JSON相关异常
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
|
||||
public Writer write(final Writer writer, final int indentFactor, final int indent, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
|
||||
.beginObj();
|
||||
this.forEach((key, value) -> {
|
||||
if (null != filter) {
|
||||
if (null != predicate) {
|
||||
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
|
||||
if (filter.accept(pair)) {
|
||||
if (predicate.test(pair)) {
|
||||
// 使用修改后的键值对
|
||||
jsonWriter.writeField(pair.getKey(), pair.getValue());
|
||||
}
|
||||
@@ -395,21 +395,21 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @param checkDuplicate 是否检查重复键,如果为{@code true},则出现重复键时抛出{@link JSONException}异常
|
||||
* @return 旧值
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private Object put(String key, Object value, final Filter<MutableEntry<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
|
||||
private Object put(String key, Object value, final Predicate<MutableEntry<String, Object>> predicate, final boolean checkDuplicate) throws JSONException {
|
||||
if (null == key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != filter) {
|
||||
if (null != predicate) {
|
||||
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
|
||||
if (filter.accept(pair)) {
|
||||
if (predicate.test(pair)) {
|
||||
// 使用修改后的键值对
|
||||
key = pair.getKey();
|
||||
value = pair.getValue();
|
||||
|
@@ -1,9 +1,10 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON字符串解析器
|
||||
*
|
||||
@@ -39,9 +40,9 @@ public class JSONParser {
|
||||
* 解析{@link JSONTokener}中的字符到目标的{@link JSONObject}中
|
||||
*
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
public void parseTo(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
public void parseTo(final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final JSONTokener tokener = this.tokener;
|
||||
|
||||
char c;
|
||||
@@ -69,7 +70,7 @@ public class JSONParser {
|
||||
throw tokener.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
|
||||
jsonObject.setOnce(key, tokener.nextValue(), filter);
|
||||
jsonObject.setOnce(key, tokener.nextValue(), predicate);
|
||||
|
||||
// Pairs are separated by ','.
|
||||
|
||||
@@ -95,9 +96,9 @@ public class JSONParser {
|
||||
* 解析JSON字符串到{@link JSONArray}中
|
||||
*
|
||||
* @param jsonArray {@link JSONArray}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null} 表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null} 表示不过滤,,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
public void parseTo(final JSONArray jsonArray, final Filter<Mutable<Object>> filter) {
|
||||
public void parseTo(final JSONArray jsonArray, final Predicate<Mutable<Object>> predicate) {
|
||||
final JSONTokener x = this.tokener;
|
||||
|
||||
if (x.nextClean() != '[') {
|
||||
@@ -108,10 +109,10 @@ public class JSONParser {
|
||||
for (; ; ) {
|
||||
if (x.nextClean() == ',') {
|
||||
x.back();
|
||||
jsonArray.addRaw(null, filter);
|
||||
jsonArray.addRaw(null, predicate);
|
||||
} else {
|
||||
x.back();
|
||||
jsonArray.addRaw(x.nextValue(), filter);
|
||||
jsonArray.addRaw(x.nextValue(), predicate);
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
case ',':
|
||||
|
@@ -5,12 +5,11 @@ import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.iter.ArrayIter;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import cn.hutool.json.serialize.JSONSerializer;
|
||||
@@ -21,6 +20,7 @@ import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 对象和JSON映射器,用于转换对象为JSON,支持:
|
||||
@@ -65,10 +65,10 @@ public class ObjectMapper {
|
||||
* 将给定对象转换为{@link JSONObject}
|
||||
*
|
||||
* @param jsonObject 目标{@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void map(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
public void map(final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final Object source = this.source;
|
||||
if (null == source) {
|
||||
return;
|
||||
@@ -89,29 +89,29 @@ public class ObjectMapper {
|
||||
if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), predicate, false);
|
||||
}
|
||||
} else if (source instanceof Map.Entry) {
|
||||
final Map.Entry entry = (Map.Entry) source;
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), filter, false);
|
||||
jsonObject.set(Convert.toStr(entry.getKey()), entry.getValue(), predicate, false);
|
||||
} else if (source instanceof CharSequence) {
|
||||
// 可能为JSON字符串
|
||||
mapFromStr((CharSequence) source, jsonObject, filter);
|
||||
mapFromStr((CharSequence) source, jsonObject, predicate);
|
||||
} else if (source instanceof Reader) {
|
||||
mapFromTokener(new JSONTokener((Reader) source, jsonObject.getConfig()), jsonObject, filter);
|
||||
mapFromTokener(new JSONTokener((Reader) source, jsonObject.getConfig()), jsonObject, predicate);
|
||||
} else if (source instanceof InputStream) {
|
||||
mapFromTokener(new JSONTokener((InputStream) source, jsonObject.getConfig()), jsonObject, filter);
|
||||
mapFromTokener(new JSONTokener((InputStream) source, jsonObject.getConfig()), jsonObject, predicate);
|
||||
} else if (source instanceof byte[]) {
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonObject.getConfig()), jsonObject, filter);
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonObject.getConfig()), jsonObject, predicate);
|
||||
} else if (source instanceof JSONTokener) {
|
||||
// JSONTokener
|
||||
mapFromTokener((JSONTokener) source, jsonObject, filter);
|
||||
mapFromTokener((JSONTokener) source, jsonObject, predicate);
|
||||
} else if (source instanceof ResourceBundle) {
|
||||
// JSONTokener
|
||||
mapFromResourceBundle((ResourceBundle) source, jsonObject, filter);
|
||||
mapFromResourceBundle((ResourceBundle) source, jsonObject, predicate);
|
||||
} else if (BeanUtil.isReadableBean(source.getClass())) {
|
||||
// 普通Bean
|
||||
mapFromBean(source, jsonObject, filter);
|
||||
mapFromBean(source, jsonObject, predicate);
|
||||
} else {
|
||||
// 不支持对象类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
@@ -122,11 +122,11 @@ public class ObjectMapper {
|
||||
* 将给定对象转换为{@link JSONArray}
|
||||
*
|
||||
* @param jsonArray 目标{@link JSONArray}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @throws JSONException 非数组或集合
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void map(final JSONArray jsonArray, final Filter<Mutable<Object>> filter) throws JSONException {
|
||||
public void map(final JSONArray jsonArray, final Predicate<Mutable<Object>> predicate) throws JSONException {
|
||||
final Object source = this.source;
|
||||
if (null == source) {
|
||||
return;
|
||||
@@ -138,15 +138,15 @@ public class ObjectMapper {
|
||||
serializer.serialize(jsonArray, source);
|
||||
} else if (source instanceof CharSequence) {
|
||||
// JSON字符串
|
||||
mapFromStr((CharSequence) source, jsonArray, filter);
|
||||
mapFromStr((CharSequence) source, jsonArray, predicate);
|
||||
} else if (source instanceof Reader) {
|
||||
mapFromTokener(new JSONTokener((Reader) source, jsonArray.getConfig()), jsonArray, filter);
|
||||
mapFromTokener(new JSONTokener((Reader) source, jsonArray.getConfig()), jsonArray, predicate);
|
||||
} else if (source instanceof InputStream) {
|
||||
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter);
|
||||
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, predicate);
|
||||
} else if (source instanceof byte[]) {
|
||||
final byte[] bytesSource = (byte[]) source;
|
||||
if ('[' == bytesSource[0] && ']' == bytesSource[bytesSource.length - 1]) {
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream(bytesSource), jsonArray.getConfig()), jsonArray, filter);
|
||||
mapFromTokener(new JSONTokener(IoUtil.toStream(bytesSource), jsonArray.getConfig()), jsonArray, predicate);
|
||||
} else {
|
||||
// https://github.com/dromara/hutool/issues/2369
|
||||
// 非标准的二进制流,则按照普通数组对待
|
||||
@@ -155,7 +155,7 @@ public class ObjectMapper {
|
||||
}
|
||||
}
|
||||
} else if (source instanceof JSONTokener) {
|
||||
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
||||
mapFromTokener((JSONTokener) source, jsonArray, predicate);
|
||||
} else {
|
||||
final Iterator<?> iter;
|
||||
if (ArrayUtil.isArray(source)) {// 数组
|
||||
@@ -174,7 +174,7 @@ public class ObjectMapper {
|
||||
next = iter.next();
|
||||
// 检查循环引用
|
||||
if (next != source) {
|
||||
jsonArray.addRaw(JSONUtil.wrap(next, config), filter);
|
||||
jsonArray.addRaw(JSONUtil.wrap(next, config), predicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,15 +185,15 @@ public class ObjectMapper {
|
||||
*
|
||||
* @param bundle ResourceBundle
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @since 5.3.1
|
||||
*/
|
||||
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final Enumeration<String> keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
final String key = keys.nextElement();
|
||||
if (key != null) {
|
||||
InternalJSONUtil.propertyPut(jsonObject, key, bundle.getString(key), filter);
|
||||
InternalJSONUtil.propertyPut(jsonObject, key, bundle.getString(key), predicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,16 +203,16 @@ public class ObjectMapper {
|
||||
*
|
||||
* @param source JSON字符串
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final String jsonStr = StrUtil.trim(source);
|
||||
if (StrUtil.startWith(jsonStr, '<')) {
|
||||
// 可能为XML
|
||||
XML.toJSONObject(jsonObject, jsonStr, false);
|
||||
return;
|
||||
}
|
||||
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonObject.getConfig()), jsonObject, filter);
|
||||
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonObject.getConfig()), jsonObject, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,11 +220,11 @@ public class ObjectMapper {
|
||||
*
|
||||
* @param source JSON字符串
|
||||
* @param jsonArray {@link JSONArray}
|
||||
* @param filter 值过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 值过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤,,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
private void mapFromStr(final CharSequence source, final JSONArray jsonArray, final Filter<Mutable<Object>> filter) {
|
||||
private void mapFromStr(final CharSequence source, final JSONArray jsonArray, final Predicate<Mutable<Object>> predicate) {
|
||||
if (null != source) {
|
||||
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonArray.getConfig()), jsonArray, filter);
|
||||
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonArray.getConfig()), jsonArray, predicate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,10 +233,10 @@ public class ObjectMapper {
|
||||
*
|
||||
* @param x JSONTokener
|
||||
* @param jsonObject {@link JSONObject}
|
||||
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
JSONParser.of(x).parseTo(jsonObject, filter);
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
JSONParser.of(x).parseTo(jsonObject, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,22 +244,23 @@ public class ObjectMapper {
|
||||
*
|
||||
* @param x {@link JSONTokener}
|
||||
* @param jsonArray {@link JSONArray}
|
||||
* @param filter 值过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤
|
||||
* @param predicate 值过滤编辑器,可以通过实现此接口,完成解析前对值的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONArray jsonArray, final Filter<Mutable<Object>> filter) {
|
||||
JSONParser.of(x).parseTo(jsonArray, filter);
|
||||
private static void mapFromTokener(final JSONTokener x, final JSONArray jsonArray, final Predicate<Mutable<Object>> predicate) {
|
||||
JSONParser.of(x).parseTo(jsonArray, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Bean转换
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param predicate 过滤器,,{@link Predicate#test(Object)}为{@code true}保留,null表示保留。
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private static void mapFromBean(final Object bean, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
|
||||
private static void mapFromBean(final Object bean, final JSONObject jsonObject, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||
final CopyOptions copyOptions = InternalJSONUtil.toCopyOptions(jsonObject.getConfig());
|
||||
if (null != filter) {
|
||||
copyOptions.setFieldEditor((entry -> filter.accept(entry) ? entry : null));
|
||||
if (null != predicate) {
|
||||
copyOptions.setFieldEditor((entry -> predicate.test(entry) ? entry : null));
|
||||
}
|
||||
BeanUtil.beanToMap(bean, jsonObject, copyOptions);
|
||||
}
|
||||
|
Reference in New Issue
Block a user