Refine search
/** * 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()); }
/** * Creates a new configuration by adding the new options to the options used in this configuration. * @param options options to add * @return a new configuration */ public Configuration addOptions(Option... options) { EnumSet<Option> opts = EnumSet.noneOf(Option.class); opts.addAll(this.options); opts.addAll(asList(options)); return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(opts).evaluationListener(evaluationListeners).build(); }
private void saveDefaults() { try { Configuration defaultConfiguration = Configuration.defaultConfiguration(); this.jsonProvider = defaultConfiguration.jsonProvider(); this.mappingProvider = defaultConfiguration.mappingProvider(); this.options = defaultConfiguration.getOptions(); this.hadDefaults = true; } catch (NoClassDefFoundError ignore) { this.hadDefaults = false; } }
private Configuration deleteOptionFromConfiguration( Configuration config, Option option ) { Configuration currentConf = config; if ( currentConf != null ) { EnumSet<Option> currentOptions = EnumSet.noneOf( Option.class ); currentOptions.addAll( currentConf.getOptions() ); if ( currentOptions.remove( option ) ) { if ( log.isDebug() ) { log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Option.Delete", option ) ); } currentConf = Configuration.defaultConfiguration().addOptions( currentOptions.toArray( new Option[currentOptions.size()] ) ); } } if ( log.isDebug() ) { log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Options", currentConf.getOptions() ) ); } return currentConf; }
/** * 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(); }
@Test public void issue_46() { String json = "{\"a\": {}}"; Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS); assertThat((String)JsonPath.using(configuration).parse(json).read("a.x")).isNull(); try { read(json, "a.x"); failBecauseExceptionWasNotThrown(PathNotFoundException.class); } catch (PathNotFoundException e) { assertThat(e).hasMessage("No results for path: $['a']['x']"); } }
@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"); }
@Test public void when_deep_scanning_require_properties_is_ignored_on_scan_target() { final Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); Object result = JsonPath.parse("[{\"x\": {\"foo\": {\"x\": 4}, \"x\": null}, \"y\": {\"x\": 1}}, {\"x\": []}]").read( "$..x"); assertThat(result).asList().hasSize(5); List<Integer> result1 = JsonPath.using(conf).parse("{\"foo\": {\"bar\": 4}}").read("$..foo.bar"); assertThat(result1).containsExactly(4); assertEvaluationThrows("{\"foo\": {\"baz\": 4}}", "$..foo.bar", PathNotFoundException.class, conf); }
@Test public void an_indefinite_path_can_be_returned_as_list() { Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build(); List<Object> result = using(conf).parse("{\"bar\": {\"foo\": null}}").read("$..foo"); assertThat(result).hasSize(1); assertThat(result.get(0)).isNull(); assertThat((List)using(conf).parse("{\"bar\": {\"foo\": [1, 4, 8]}}").read("$..foo")).asList() .containsExactly(Arrays.asList(1, 4, 8)); }
private static Set<JsonPathResultEntry> computeResults( String jsonFile, String path, boolean includeSuffix) { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); Configuration baseConfiguration = b.build(); String jsonStr = CommonUtil.readResource(jsonFile); Object jsonObject = JsonPath.parse(jsonStr, baseConfiguration).json(); JsonPathQuery query = new JsonPathQuery(path, includeSuffix); JsonPathResult result = JsonPathAnswerer.computeResult(jsonObject, query); return new HashSet<>(result.getResult().values()); }
public JsonPathTest() { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); _baseConfiguration = b.build(); _oneNtpServerNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/oneNtpServer.json"); _oneNtpServerNodesAnswerJsonObject = JsonPath.parse(_oneNtpServerNodesAnswerStr, _baseConfiguration).json(); _twoNtpServersNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/twoNtpServers.json"); _twoNtpServersNodesAnswerJsonObject = JsonPath.parse(_twoNtpServersNodesAnswerStr, _baseConfiguration).json(); ConfigurationBuilder prefixCb = new ConfigurationBuilder(); prefixCb.mappingProvider(_baseConfiguration.mappingProvider()); prefixCb.jsonProvider(_baseConfiguration.jsonProvider()); prefixCb.evaluationListener(_baseConfiguration.getEvaluationListeners()); prefixCb.options(_baseConfiguration.getOptions()); prefixCb.options(Option.ALWAYS_RETURN_LIST); prefixCb.options(Option.AS_PATH_LIST); _prefixConfiguration = prefixCb.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"); }
return path.read(inputStream, genericFile.getCharset(), configuration); LOG.trace("JSonPath: {} is read as String: {}", path, json); String str = (String) json; return path.read(str, configuration); } else if (json instanceof Map) { LOG.trace("JSonPath: {} is read as Map: {}", path, json); Map map = (Map) json; return path.read(map, configuration); } else if (json instanceof List) { LOG.trace("JSonPath: {} is read as List: {}", path, json); if (configuration.getOptions().contains(SUPPRESS_EXCEPTIONS)) { if (configuration.getOptions().contains(ALWAYS_RETURN_LIST)) { return Collections.emptyList(); } else {
@Test public void issue_143() { String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }"; Configuration configuration = Configuration.builder().options( Option.AS_PATH_LIST ).build(); List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar")); assertThat(pathList).containsExactly("$['foo']['bar']"); }
@Test public void shouldMatchJsonPathOnParsedJsonObject() { Object json = Configuration.defaultConfiguration().jsonProvider().parse(BOOKS_JSON); assertThat(json, hasJsonPath("$.store.name", equalTo("Little Shop"))); }
@Test public void require_single_property() { List json = new ArrayList() {{ add(singletonMap("a", "a0")); add(singletonMap("b", "b2")); }}; Configuration configuration = JSON_SMART_CONFIGURATION.addOptions(Option.REQUIRE_PROPERTIES); Object result = JsonPath.using(configuration).parse(json).read("$..a"); assertThat(result).asList().containsExactly("a0"); }
@Test public void the_age_of_all_with_age_defined() { //List<Integer> result = JsonPath.read(DOCUMENT, "$.children[*].age"); List<Integer> result = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(DOCUMENT).read("$.children[*].age"); Assertions.assertThat(result).containsSequence(0, null); }
/** * {@inheritDoc} */ public JsonAsserter assertNotDefined(String path) { try { Configuration c = Configuration.defaultConfiguration(); JsonPath.using(c).parse(jsonObject).read(path); throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); } catch (PathNotFoundException e) { } return this; }
public void set(Object newVal, Configuration configuration){ for (String property : properties) { configuration.jsonProvider().setProperty(parent, property, newVal); } } public void convert(MapFunction mapFunction, Configuration configuration) {