Skip to content

Commit 12ab661

Browse files
authored
issue #721 (#722)
* A solution to fix #721, and added tests * Replace the test file
1 parent fb0d84f commit 12ab661

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.net.URL;
27+
import java.util.ArrayList;
28+
import java.util.List;
2729

2830
import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST;
2931
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
@@ -255,11 +257,24 @@ public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration confi
255257
public <T> T delete(Object jsonObject, Configuration configuration) {
256258
notNull(jsonObject, "json can not be null");
257259
notNull(configuration, "configuration can not be null");
258-
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
259-
for (PathRef updateOperation : evaluationContext.updateOperations()) {
260-
updateOperation.delete(configuration);
260+
261+
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
262+
263+
try {
264+
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
265+
for (PathRef updateOperation : evaluationContext.updateOperations()) {
266+
updateOperation.delete(configuration);
267+
}
268+
return resultByConfiguration(jsonObject, configuration, evaluationContext);
269+
} catch (RuntimeException e) {
270+
if (!optSuppressExceptions) {
271+
throw e;
272+
} else {
273+
List<String> list = new ArrayList<String>(); // the log messages
274+
list.add("delete throws "+e.getMessage()); // TODO
275+
return (T) list;
276+
}
261277
}
262-
return resultByConfiguration(jsonObject, configuration, evaluationContext);
263278
}
264279

265280
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.jayway.jsonpath;
2+
3+
import com.jayway.jsonpath.Configuration;
4+
import com.jayway.jsonpath.DocumentContext;
5+
import com.jayway.jsonpath.JsonPath;
6+
import com.jayway.jsonpath.Option;
7+
import org.junit.Test;
8+
9+
public class Issue_721 {
10+
11+
public static final Configuration jsonConf = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
12+
13+
@Test
14+
public void test_delete_1(){ // originally throws PathNotFoundException
15+
DocumentContext dc = JsonPath.using(jsonConf)
16+
.parse("{\"top\": {\"middle\": null}}")
17+
.delete(JsonPath.compile("$.top.middle.bottom"));
18+
Object ans = dc.read("$");
19+
//System.out.println(ans);
20+
assert(ans.toString().equals("{top={middle=null}}"));
21+
}
22+
23+
@Test
24+
public void test_delete_2(){ // originally passed
25+
DocumentContext dc = JsonPath.using(jsonConf)
26+
.parse("[" +
27+
"{\"top\": {\"middle\": null}}," +
28+
"{\"top\": {\"middle\": {} }}," +
29+
"{\"top\": {\"middle\": {bottom: 2} }}," +
30+
"]")
31+
.delete(JsonPath.compile("$[*].top.middle.bottom"));
32+
Object ans = dc.read("$");
33+
//System.out.println(ans);
34+
assert(ans.toString().equals("[{\"top\":{\"middle\":null}},{\"top\":{\"middle\":{}}},{\"top\":{\"middle\":{}}}]"));
35+
}
36+
}

0 commit comments

Comments
 (0)