remove pair

This commit is contained in:
Looly
2022-05-12 00:30:40 +08:00
parent 1661ec42bd
commit eaa80f55a6
15 changed files with 187 additions and 234 deletions

View File

@@ -3,7 +3,7 @@ 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.MutablePair;
import cn.hutool.core.lang.mutable.MutableEntry;
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
import cn.hutool.core.map.CaseInsensitiveTreeMap;
import cn.hutool.core.util.ArrayUtil;
@@ -140,7 +140,7 @@ public final class InternalJSONUtil {
* @param value 值
* @return JSONObject
*/
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Filter<MutablePair<String, Object>> filter) {
static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Filter<MutableEntry<String, Object>> filter) {
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
final int last = path.length - 1;
JSONObject target = jsonObject;

View File

@@ -5,7 +5,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Filter;
import cn.hutool.core.lang.mutable.Mutable;
import cn.hutool.core.lang.mutable.MutableObj;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.lang.mutable.MutableEntry;
import cn.hutool.core.text.StrJoiner;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.serialize.JSONWriter;
@@ -416,10 +416,10 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @return 替换的值,即之前的值
* @since 5.8.0
*/
public Object set(final int index, Object element, final Filter<MutablePair<Integer, Object>> filter) {
public Object set(final int index, Object element, final Filter<MutableEntry<Integer, Object>> filter) {
// 添加前置过滤通过MutablePair实现过滤、修改键值对等
if (null != filter) {
final MutablePair<Integer, Object> pair = new MutablePair<>(index, element);
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, element);
if (filter.accept(pair)) {
// 使用修改后的值
element = pair.getValue();
@@ -515,7 +515,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @return JSON字符串
* @since 5.7.15
*/
public String toJSONString(final int indentFactor, final Filter<MutablePair<Integer, Object>> filter) {
public String toJSONString(final int indentFactor, final Filter<MutableEntry<Integer, Object>> filter) {
final StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0, filter).toString();
@@ -539,12 +539,12 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @throws JSONException JSON相关异常
* @since 5.7.15
*/
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutablePair<Integer, Object>> filter) throws JSONException {
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<Integer, Object>> filter) throws JSONException {
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginArray();
CollUtil.forEach(this, (value, index) -> {
final MutablePair<Integer, Object> pair = new MutablePair<>(index, value);
final MutableEntry<Integer, Object> pair = new MutableEntry<>(index, value);
if (null == filter || filter.accept(pair)) {
jsonWriter.writeValue(pair.getValue());
}

View File

@@ -3,7 +3,7 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Filter;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.lang.mutable.MutableEntry;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.map.MapWrapper;
import cn.hutool.core.util.ObjUtil;
@@ -126,7 +126,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
* @since 5.8.0
*/
public JSONObject(final Object source, final JSONConfig config, final Filter<MutablePair<String, Object>> filter) {
public JSONObject(final Object source, final JSONConfig config, final Filter<MutableEntry<String, Object>> filter) {
this(DEFAULT_CAPACITY, config);
ObjectMapper.of(source).map(this, filter);
}
@@ -233,7 +233,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException 值是无穷数字抛出此异常
* @since 5.8.0
*/
public JSONObject set(final String key, final Object value, final Filter<MutablePair<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
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);
return this;
}
@@ -260,7 +260,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException 值是无穷数字、键重复抛出异常
* @since 5.8.0
*/
public JSONObject setOnce(final String key, final Object value, final Filter<MutablePair<String, Object>> filter) throws JSONException {
public JSONObject setOnce(final String key, final Object value, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
return set(key, value, filter, true);
}
@@ -378,7 +378,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @return JSON字符串
* @since 5.7.15
*/
public String toJSONString(final int indentFactor, final Filter<MutablePair<String, Object>> filter) {
public String toJSONString(final int indentFactor, final Filter<MutableEntry<String, Object>> filter) {
final StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0, filter).toString();
@@ -402,12 +402,12 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException JSON相关异常
* @since 5.7.15
*/
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutablePair<String, Object>> filter) throws JSONException {
public Writer write(final Writer writer, final int indentFactor, final int indent, final Filter<MutableEntry<String, Object>> filter) throws JSONException {
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginObj();
this.forEach((key, value) -> {
if (null != filter) {
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
if (filter.accept(pair)) {
// 使用修改后的键值对
jsonWriter.writeField(pair.getKey(), pair.getValue());
@@ -439,14 +439,14 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @throws JSONException 值是无穷数字抛出此异常
* @since 5.8.0
*/
private Object put(String key, Object value, final Filter<MutablePair<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
private Object put(String key, Object value, final Filter<MutableEntry<String, Object>> filter, final boolean checkDuplicate) throws JSONException {
if (null == key) {
return this;
}
// 添加前置过滤通过MutablePair实现过滤、修改键值对等
if (null != filter) {
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
final MutableEntry<String, Object> pair = new MutableEntry<>(key, value);
if (filter.accept(pair)) {
// 使用修改后的键值对
key = pair.getKey();

View File

@@ -2,7 +2,7 @@ package cn.hutool.json;
import cn.hutool.core.lang.func.Filter;
import cn.hutool.core.lang.mutable.Mutable;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.lang.mutable.MutableEntry;
/**
* JSON字符串解析器
@@ -41,7 +41,7 @@ public class JSONParser {
* @param jsonObject {@link JSONObject}
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
*/
public void parseTo(final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
public void parseTo(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
final JSONTokener tokener = this.tokener;
char c;

View File

@@ -6,7 +6,7 @@ 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.MutablePair;
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;
@@ -67,7 +67,7 @@ public class ObjectMapper {
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public void map(final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
public void map(final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
final Object source = this.source;
if (null == source) {
return;
@@ -179,7 +179,7 @@ public class ObjectMapper {
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
* @since 5.3.1
*/
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
private static void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
final Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
final String key = keys.nextElement();
@@ -196,7 +196,7 @@ public class ObjectMapper {
* @param jsonObject {@link JSONObject}
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤
*/
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
private static void mapFromStr(final CharSequence source, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
final String jsonStr = StrUtil.trim(source);
if (StrUtil.startWith(jsonStr, '<')) {
// 可能为XML
@@ -226,7 +226,7 @@ public class ObjectMapper {
* @param jsonObject {@link JSONObject}
* @param filter 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作
*/
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Filter<MutablePair<String, Object>> filter) {
private static void mapFromTokener(final JSONTokener x, final JSONObject jsonObject, final Filter<MutableEntry<String, Object>> filter) {
JSONParser.of(x).parseTo(jsonObject, filter);
}