forked from alibaba/fastjson2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSONPath add static method 'remove', JSONPath.set & JSONPath.remove s…
…upport more syntax
- Loading branch information
Showing
4 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPathRemoveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.alibaba.fastjson2.jsonpath; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONArray; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import com.alibaba.fastjson2.JSONPath; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class JSONPathRemoveTest { | ||
@Test | ||
public void test() { | ||
JSONArray array = JSON.parseArray("[0,1,2,3]"); | ||
JSONPath.remove(array, "$[?(@ == 1)]"); | ||
assertEquals("[0,2,3]", array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1']}}"); | ||
JSONPath.remove(object, "$.store.book[0]"); | ||
assertEquals("{\"store\":{\"book\":[\"x1\"]}}", object.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1','x2','x3']}}"); | ||
JSONPath.remove(object, "$.store.book[0:2]"); | ||
assertEquals("{\"store\":{\"book\":[\"x2\",\"x3\"]}}", object.toJSONString()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
core/src/test/java/com/alibaba/fastjson2/jsonpath/JSONPathSetTest1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.alibaba.fastjson2.jsonpath; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONArray; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import com.alibaba.fastjson2.JSONPath; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class JSONPathSetTest1 { | ||
@Test | ||
public void test() { | ||
JSONArray array = JSON.parseArray("[0,1,2,3]"); | ||
JSONPath.set(array, "$[?(@ == 1)]", 9); | ||
assertEquals("[0,9,2,3]", array.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1']}}"); | ||
JSONPath.set(object, "$.store.book[0]", "a"); | ||
assertEquals("{\"store\":{\"book\":[\"a\",\"x1\"]}}", object.toJSONString()); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1','x2','x3']}}"); | ||
JSONPath.set(object, "$.store.book[0:2]", "a"); | ||
assertEquals("{\"store\":{\"book\":[\"a\",\"a\",\"x2\",\"x3\"]}}", object.toJSONString()); | ||
} | ||
} |