This commit is contained in:
Looly
2022-08-30 22:22:25 +08:00
parent c5911c0501
commit 305737939e
7 changed files with 109 additions and 30 deletions

View File

@@ -0,0 +1,26 @@
package cn.hutool.json;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* Predicate多层过滤
*/
public class IssueI5OMSCTest {
@Test
public void filterTest(){
final JSONObject json = JSONUtil.parseObj(ResourceUtil.readUtf8Str("issueI5OMSC.json"));
final String s = json.toJSONString(0, (entry) -> {
final Object key = entry.getKey();
if(key instanceof String){
return ListUtil.of("store", "bicycle", "color", "book", "author").contains(key);
}
return true;
});
Assert.assertEquals("{\"store\":{\"bicycle\":{\"color\":\"red\"},\"book\":[{\"author\":\"Evelyn Waugh\"},{\"author\":\"Evelyn Waugh02\"}]}}", s);
}
}

View File

@@ -698,7 +698,7 @@ public class JSONObjectTest {
.set("d", true);
final String s = json1.toJSONString(0, (pair) -> {
pair.setKey(StrUtil.toUnderlineCase(pair.getKey()));
pair.setKey(StrUtil.toUnderlineCase(pair.getKey().toString()));
return true;
});
Assert.assertEquals("{\"a_key\":\"value1\",\"b_job\":\"value2\",\"c_good\":\"value3\",\"d\":true}", s);

View File

@@ -0,0 +1,32 @@
{
"store": {
"bicycle": {
"color": "red",
"price": 19.95
},
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"items": [
{
"name": "wujing001",
"age": 18
},
{
"name": "wujing002",
"age": 18
}
]
},
{
"category": "fiction02",
"author": "Evelyn Waugh02",
"title": "Sword of Honour02",
"price": 12.99
}
]
}
}