This commit is contained in:
Looly
2022-02-13 22:04:02 +08:00
parent 9439ec4228
commit 0189de2dad
3 changed files with 30 additions and 26 deletions

View File

@@ -592,9 +592,14 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginObj();
this.forEach((key, value) -> {
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
if (null == filter || filter.accept(pair)) {
jsonWriter.writeField(pair.getKey(), pair.getValue());
if (null != filter){
final MutablePair<String, Object> pair = new MutablePair<>(key, value);
if (filter.accept(pair)) {
// 使用修改后的键值对
jsonWriter.writeField(pair.getKey(), pair.getValue());
}
} else {
jsonWriter.writeField(key, value);
}
});
jsonWriter.end();

View File

@@ -30,7 +30,6 @@ import org.junit.Test;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -658,6 +657,21 @@ public class JSONObjectTest {
Assert.assertEquals("{\"b\":\"value2_edit\"}", s);
}
@Test
public void toUnderLineCaseTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true))
.set("aKey", "value1")
.set("bJob", "value2")
.set("cGood", "value3")
.set("d", true);
final String s = json1.toJSONString(0, (pair) -> {
pair.setKey(StrUtil.toUnderlineCase(pair.getKey()));
return true;
});
Assert.assertEquals("{\"a_key\":\"value1\",\"b_job\":\"value2\",\"c_good\":\"value3\",\"d\":true}", s);
}
@Test
public void nullToEmptyTest() {
JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setOrder(true).setIgnoreNullValue(false))