nl.hsac.fitnesse.fixture.util.JsonPathHelper Maven / Gradle / Ivy
package nl.hsac.fitnesse.fixture.util;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.PathNotFoundException;
import java.util.Collections;
import java.util.List;
/**
* Helper to evaluate JsonPath expressions against a JSON object.
* @link https://github.com/jayway/JsonPath
* @link http://jsonpath.herokuapp.com/
*/
public class JsonPathHelper {
private final static Configuration CONF = Configuration
.defaultConfiguration()
.addOptions(Option.SUPPRESS_EXCEPTIONS);
private final static ParseContext CONTEXT = JsonPath.using(CONF);
private String lastJson;
private DocumentContext lastContext;
/**
* Evaluates a JsonPath expression returning a single element.
* @param json JSON value.
* @param jsonPath expression to evaluate.
* @return result result of expression.
* @throws java.lang.RuntimeException if jsonPath would return multiple elements.
*/
public Object getJsonPath(String json, String jsonPath) {
if (!JsonPath.isPathDefinite(jsonPath)) {
throw new RuntimeException(jsonPath + " returns a list of results, not a single.");
}
return parseJson(json).read(jsonPath);
}
/**
* Evaluates a JsonPath expression returning a multiple elements.
* @param json JSON value.
* @param jsonPath expression to evaluate.
* @return result results of expression.
* @throws java.lang.RuntimeException if jsonPath would return a single element.
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy