Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #721 #722

Merged
merged 2 commits into from Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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\":{}}}]"));
}
}