add methods

This commit is contained in:
Looly 2021-10-15 01:27:16 +08:00
parent 05141a7927
commit 5cd7e806e1
5 changed files with 132 additions and 3 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 增加RingIndexUtilpr#438@Gitee
* 【core 】 Assert增加checkBetween重载pr#436@Gitee
* 【core 】 ReUtil增加命名分组重载pr#439@Gitee
* 【json 】 toString和writer增加Filterissue#I4DQNQ@Gitee
*
### 🐞Bug修复
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题issue#1885@Github

View File

@ -3,6 +3,8 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.ArrayIter;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.text.StrJoiner;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
@ -12,6 +14,7 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
import cn.hutool.json.serialize.JSONSerializer;
import cn.hutool.json.serialize.JSONWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
@ -529,11 +532,48 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
return this.toJSONString(0);
}
/**
* 返回JSON字符串<br>
* 支持过滤器即选择哪些字段或值不写出
*
* @param indentFactor 每层缩进空格数
* @param filter 键值对过滤器
* @return JSON字符串
* @since 5.7.15
*/
public String toJSONString(int indentFactor, Filter<Pair<Integer, Object>> filter){
final StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0, filter).toString();
}
}
@Override
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
return write(writer, indentFactor, indent, null);
}
/**
* 将JSON内容写入Writer<br>
* 支持过滤器即选择哪些字段或值不写出
*
* @param writer writer
* @param indentFactor 缩进因子定义每一级别增加的缩进量
* @param indent 本级别缩进量
* @param filter 过滤器
* @return Writer
* @throws JSONException JSON相关异常
* @since 5.7.15
*/
public Writer write(Writer writer, int indentFactor, int indent, Filter<Pair<Integer, Object>> filter) throws JSONException {
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginArray();
this.forEach(jsonWriter::writeValue);
CollUtil.forEach(this, (value, index)->{
if (null == filter || filter.accept(new Pair<>(index, value))) {
jsonWriter.writeValue(value);
}
});
jsonWriter.end();
// 此处不关闭Writer考虑writer后续还需要填内容
return writer;

View File

@ -6,6 +6,8 @@ import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.CaseInsensitiveLinkedMap;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.map.MapUtil;
@ -18,6 +20,7 @@ import cn.hutool.json.serialize.JSONObjectSerializer;
import cn.hutool.json.serialize.JSONSerializer;
import cn.hutool.json.serialize.JSONWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
@ -552,17 +555,54 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
return this.toJSONString(0);
}
/**
* 返回JSON字符串<br>
* 支持过滤器即选择哪些字段或值不写出
*
* @param indentFactor 每层缩进空格数
* @param filter 键值对过滤器
* @return JSON字符串
* @since 5.7.15
*/
public String toJSONString(int indentFactor, Filter<Pair<String, Object>> filter){
final StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0, filter).toString();
}
}
@Override
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
return write(writer, indentFactor, indent, null);
}
/**
* 将JSON内容写入Writer<br>
* 支持过滤器即选择哪些字段或值不写出
*
* @param writer writer
* @param indentFactor 缩进因子定义每一级别增加的缩进量
* @param indent 本级别缩进量
* @param filter 过滤器
* @return Writer
* @throws JSONException JSON相关异常
* @since 5.7.15
*/
public Writer write(Writer writer, int indentFactor, int indent, Filter<Pair<String, Object>> filter) throws JSONException {
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginObj();
this.forEach(jsonWriter::writeField);
this.forEach((key, value) -> {
if (null == filter || filter.accept(new Pair<>(key, value))) {
jsonWriter.writeField(key, value);
}
});
jsonWriter.end();
// 此处不关闭Writer考虑writer后续还需要填内容
return writer;
}
// ------------------------------------------------------------------------------------------------- Private method start
/**
* Bean对象转Map
*

View File

@ -239,4 +239,28 @@ public class JSONArrayTest {
private Integer id;
private String name;
}
@Test
public void filterIncludeTest(){
JSONArray json1 = JSONUtil.createArray()
.set("value1")
.set("value2")
.set("value3")
.set(true);
final String s = json1.toJSONString(0, (pair) -> pair.getValue().equals("value2"));
Assert.assertEquals("[\"value2\"]", s);
}
@Test
public void filterExcludeTest(){
JSONArray json1 = JSONUtil.createArray()
.set("value1")
.set("value2")
.set("value3")
.set(true);
final String s = json1.toJSONString(0, (pair) -> false == pair.getValue().equals("value2"));
Assert.assertEquals("[\"value1\",\"value3\",true]", s);
}
}

View File

@ -609,4 +609,28 @@ public class JSONObjectTest {
class BigDecimalBean{
private BigDecimal orderId;
}
@Test
public void filterIncludeTest(){
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true))
.set("a", "value1")
.set("b", "value2")
.set("c", "value3")
.set("d", true);
final String s = json1.toJSONString(0, (pair) -> pair.getKey().equals("b"));
Assert.assertEquals("{\"b\":\"value2\"}", s);
}
@Test
public void filterExcludeTest(){
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true))
.set("a", "value1")
.set("b", "value2")
.set("c", "value3")
.set("d", true);
final String s = json1.toJSONString(0, (pair) -> false == pair.getKey().equals("b"));
Assert.assertEquals("{\"a\":\"value1\",\"c\":\"value3\",\"d\":true}", s);
}
}