mirror of
https://gitee.com/dromara/hutool.git
synced 2025-10-26 10:49:27 +08:00
add filter
This commit is contained in:
@@ -218,7 +218,7 @@ public class JSONArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putTest(){
|
||||
public void putToIndexTest(){
|
||||
final JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(3, "test");
|
||||
// 第三个位置插入值,0~2都是null
|
||||
@@ -279,4 +279,30 @@ public class JSONArrayTest {
|
||||
|
||||
Assert.assertEquals("[null]", array.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFilterTest() {
|
||||
String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> mutable.get().toString().contains("111"));
|
||||
Assert.assertEquals(1, array.size());
|
||||
Assert.assertTrue(array.getJSONObject(0).containsKey("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFilterEditTest() {
|
||||
String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> {
|
||||
final JSONObject o = new JSONObject(mutable.get());
|
||||
if("111".equals(o.getStr("id"))){
|
||||
o.set("name", "test1_edit");
|
||||
}
|
||||
mutable.set(o);
|
||||
return true;
|
||||
});
|
||||
Assert.assertEquals(2, array.size());
|
||||
Assert.assertTrue(array.getJSONObject(0).containsKey("id"));
|
||||
Assert.assertEquals("test1_edit", array.getJSONObject(0).get("name"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -682,4 +682,26 @@ public class JSONObjectTest {
|
||||
});
|
||||
Assert.assertEquals("{\"a\":\"\",\"b\":\"value2\"}", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFilterTest() {
|
||||
String jsonStr = "{\"b\":\"value2\",\"c\":\"value3\",\"a\":\"value1\", \"d\": true, \"e\": null}";
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
JSONObject jsonObject = new JSONObject(jsonStr, null, (pair)-> "b".equals(pair.getKey()));
|
||||
Assert.assertEquals(1, jsonObject.size());
|
||||
Assert.assertEquals("value2", jsonObject.get("b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFilterEditTest() {
|
||||
String jsonStr = "{\"b\":\"value2\",\"c\":\"value3\",\"a\":\"value1\", \"d\": true, \"e\": null}";
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
JSONObject jsonObject = new JSONObject(jsonStr, null, (pair)-> {
|
||||
if("b".equals(pair.getKey())){
|
||||
pair.setValue(pair.getValue() + "_edit");
|
||||
}
|
||||
return true;
|
||||
});
|
||||
Assert.assertEquals("value2_edit", jsonObject.get("b"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user