Skip to content

Commit

Permalink
issue #721 (#722)
Browse files Browse the repository at this point in the history
* A solution to fix #721, and added tests

* Replace the test file
  • Loading branch information
hezonghan committed Jun 2, 2021
1 parent fb0d84f commit 12ab661
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
23 changes: 19 additions & 4 deletions json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Expand Up @@ -24,6 +24,8 @@
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;
Expand Down Expand Up @@ -255,11 +257,24 @@ public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration confi
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);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions 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\":{}}}]"));
}
}

0 comments on commit 12ab661

Please sign in to comment.