add filter

This commit is contained in:
Looly
2022-04-16 11:44:01 +08:00
parent aa48d3f2ff
commit 6acf0854d8
7 changed files with 274 additions and 135 deletions

View File

@@ -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"));
}
}

View File

@@ -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"));
}
}