private boolean pathIsIndefinite() { return !this.jsonPath.isDefinite(); }
/** * Creates a new configuration based on default values * @return a new configuration based on defaults */ public static Configuration defaultConfiguration() { Defaults defaults = getEffectiveDefaults(); return Configuration.builder().jsonProvider(defaults.jsonProvider()).options(defaults.options()).build(); }
public JsonPathSelector(String jsonPathStr) { this.jsonPathStr = jsonPathStr; this.jsonPath = JsonPath.compile(this.jsonPathStr); }
/** * Applies this JsonPath to the provided json string * * @param json a json string * @param <T> expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) public <T> T read(String json) { return read(json, Configuration.defaultConfiguration()); }
@Test(expected = PathNotFoundException.class) public void multi_props_can_be_required() { Map<String, Object> model = new HashMap<String, Object>(){{ put("a", "a-val"); put("b", "b-val"); put("c", "c-val"); }}; Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); using(conf).parse(model).read("$['a', 'x']", Map.class); }
/** * Creates a new Configuration based on the given {@link com.jayway.jsonpath.spi.json.JsonProvider} * @param newJsonProvider json provider to use in new configuration * @return a new configuration */ public Configuration jsonProvider(JsonProvider newJsonProvider) { return Configuration.builder().jsonProvider(newJsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
@Test(expected = PathNotFoundException.class) public void issue_22() throws Exception { Configuration configuration = Configuration.defaultConfiguration(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; JsonPath.parse(json, configuration).read("a.d"); }
/** * Variant of {@link #evaluateJsonPath(String)} with a target type. * This can be useful for matching numbers reliably for example coercing an * integer into a double. * @param content the content to evaluate against * @return the result of the evaluation * @throws AssertionError if the evaluation fails */ public Object evaluateJsonPath(String content, Class<?> targetType) { try { return JsonPath.parse(content).read(this.expression, targetType); } catch (Throwable ex) { String message = "No value at JSON path \"" + this.expression + "\""; throw new AssertionError(message, ex); } }
public static void assertEvaluationThrows(final String json, final String path, Class<? extends JsonPathException> expected) { assertEvaluationThrows(json, path, expected, Configuration.defaultConfiguration()); }
public Configuration build() { if (jsonProvider == null || mappingProvider == null) { final Defaults defaults = getEffectiveDefaults(); if (jsonProvider == null) { jsonProvider = defaults.jsonProvider(); } if (mappingProvider == null){ mappingProvider = defaults.mappingProvider(); } } return new Configuration(jsonProvider, mappingProvider, options, evaluationListener); } }
/** * Applies this JsonPath to the provided json URL * * @param jsonURL url to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(URL jsonURL) throws IOException { return read(jsonURL, Configuration.defaultConfiguration()); }
@Test(expected = PathNotFoundException.class) public void missing_prop() { //Object read = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.THROW_ON_MISSING_PROPERTY)).parse(DOCUMENT).read("$.store.book[*].fooBar"); //Object read = JsonPath.using(Configuration.defaultConfiguration()).parse(DOCUMENT).read("$.store.book[*].fooBar"); Object read2 = JsonPath.using(Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(DOCUMENT).read("$.store.book[*].fooBar.not"); }
/** * Creates a new Configuration based on the given {@link com.jayway.jsonpath.spi.mapper.MappingProvider} * @param newMappingProvider mapping provider to use in new configuration * @return a new configuration */ public Configuration mappingProvider(MappingProvider newMappingProvider) { return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(newMappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
/** * Construct a new {@code JsonPathExpectationsHelper}. * @param expression the {@link JsonPath} expression; never {@code null} or empty * @param args arguments to parameterize the {@code JsonPath} expression with, * using formatting specifiers defined in {@link String#format(String, Object...)} */ public JsonPathExpectationsHelper(String expression, Object... args) { Assert.hasText(expression, "expression must not be null or empty"); this.expression = String.format(expression, args); this.jsonPath = JsonPath.compile(this.expression); }
/** * Applies this JsonPath to the provided json input stream * * @param jsonInputStream input stream to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(InputStream jsonInputStream) throws IOException { return read(jsonInputStream, Configuration.defaultConfiguration()); }
/** * Creates a new Configuration with the provided evaluation listeners * @param evaluationListener listeners * @return a new configuration */ public Configuration setEvaluationListeners(EvaluationListener... evaluationListener){ return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build(); }
/** * Applies this JsonPath to the provided json file * * @param jsonFile file to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(File jsonFile) throws IOException { return read(jsonFile, Configuration.defaultConfiguration()); }
/** * Creates a new configuration with the provided options. Options in this configuration are discarded. * @param options * @return */ public Configuration setOptions(Option... options) { return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
/** * Applies this JsonPath to the provided json document. * Note that the document must be identified as either a List or Map by * the {@link JsonProvider} * * @param jsonObject a container Object * @param <T> expected return type * @return object(s) matched by the given path */ @SuppressWarnings({"unchecked"}) public <T> T read(Object jsonObject) { return read(jsonObject, Configuration.defaultConfiguration()); }
/** * Creates a new Configuration by the provided evaluation listeners to the current listeners * @param evaluationListener listeners * @return a new configuration */ public Configuration addEvaluationListeners(EvaluationListener... evaluationListener){ return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build(); }