Browse Source

issue #721 (#722)

* A solution to fix #721, and added tests

* Replace the test file
pull/732/head
hezonghan 3 years ago committed by GitHub
parent
commit
12ab6619aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 36
      json-path/src/test/java/com/jayway/jsonpath/Issue_721.java

23
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -24,6 +24,8 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST;
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
@ -255,11 +257,24 @@ public class JsonPath {
public <T> T delete(Object jsonObject, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.delete(configuration);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.delete(configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
} catch (RuntimeException e) {
if (!optSuppressExceptions) {
throw e;
} else {
List<String> list = new ArrayList<String>(); // the log messages
list.add("delete throws "+e.getMessage()); // TODO
return (T) list;
}
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
/**

36
json-path/src/test/java/com/jayway/jsonpath/Issue_721.java

@ -0,0 +1,36 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import org.junit.Test;
public class Issue_721 {
public static final Configuration jsonConf = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
@Test
public void test_delete_1(){ // originally throws PathNotFoundException
DocumentContext dc = JsonPath.using(jsonConf)
.parse("{\"top\": {\"middle\": null}}")
.delete(JsonPath.compile("$.top.middle.bottom"));
Object ans = dc.read("$");
//System.out.println(ans);
assert(ans.toString().equals("{top={middle=null}}"));
}
@Test
public void test_delete_2(){ // originally passed
DocumentContext dc = JsonPath.using(jsonConf)
.parse("[" +
"{\"top\": {\"middle\": null}}," +
"{\"top\": {\"middle\": {} }}," +
"{\"top\": {\"middle\": {bottom: 2} }}," +
"]")
.delete(JsonPath.compile("$[*].top.middle.bottom"));
Object ans = dc.read("$");
//System.out.println(ans);
assert(ans.toString().equals("[{\"top\":{\"middle\":null}},{\"top\":{\"middle\":{}}},{\"top\":{\"middle\":{}}}]"));
}
}
Loading…
Cancel
Save