Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class EvaluationContextImpl implements EvaluationContext {
private final List<PathRef> updateOperations;
private final HashMap<Path, Object> documentEvalCache = new HashMap<Path, Object>();
private final boolean forUpdate;
private final boolean suppressExceptions;
private int resultIndex = 0;


Expand All @@ -61,7 +62,8 @@ public EvaluationContextImpl(Path path, Object rootDocument, Configuration confi
this.configuration = configuration;
this.valueResult = configuration.jsonProvider().createArray();
this.pathResult = configuration.jsonProvider().createArray();
this.updateOperations = new ArrayList<PathRef>();
this.updateOperations = new ArrayList<>();
this.suppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
}

public HashMap<Path, Object> documentEvalCache() {
Expand Down Expand Up @@ -129,7 +131,10 @@ public <T> T getValue() {
@Override
public <T> T getValue(boolean unwrap) {
if (path.isDefinite()) {
if(resultIndex == 0){
if(resultIndex == 0) {
if (suppressExceptions) {
return null;
}
throw new PathNotFoundException("No results for path: " + path.toString());
}
int len = jsonProvider().length(valueResult);
Expand All @@ -145,7 +150,10 @@ public <T> T getValue(boolean unwrap) {
@SuppressWarnings("unchecked")
@Override
public <T> T getPath() {
if(resultIndex == 0){
if(resultIndex == 0) {
if (suppressExceptions) {
return null;
}
throw new PathNotFoundException("No results for path: " + path.toString());
}
return (T)pathResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jayway.jsonpath;

import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.junit.Test;

import static org.junit.Assert.assertNull;


public class TestSuppressExceptions {

@Test
public void testSuppressExceptionsIsRespected() {
ParseContext parseContext = JsonPath.using(
new Configuration.ConfigurationBuilder().jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider()).options(Option.SUPPRESS_EXCEPTIONS)
.build());
String json = "{}";
assertNull(parseContext.parse(json).read(JsonPath.compile("$.missing")));
}

@Test
public void testSuppressExceptionsIsRespectedPath() {
ParseContext parseContext = JsonPath.using(
new Configuration.ConfigurationBuilder().jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider()).options(Option.SUPPRESS_EXCEPTIONS, Option.AS_PATH_LIST)
.build());
String json = "{}";
assertNull(parseContext.parse(json).read(JsonPath.compile("$.missing")));
}
}