@Override public DocumentContext delete(JsonPath path) { List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST)); if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Delete path {}", p); } } return this; }
@Override public DocumentContext add(JsonPath path, Object value) { List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST)); if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Add path {} new value {}", p, value); } } return this; }
@Override public DocumentContext set(JsonPath path, Object newValue) { List<String> modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST)); if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Set path {} new value {}", p, newValue); } } return this; }
@Override public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) { List<String> modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST)); if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Rename path {} new value {}", p, newKeyName); } } return this; }
@Override public DocumentContext put(JsonPath path, String key, Object value) { List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST)); if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Put path {} key {} value {}", p, key, value); } } return this; }
protected FastJsonReader( LogChannelInterface log ) throws KettleException { this.ignoreMissingPath = false; this.defaultPathLeafToNull = true; this.jsonConfiguration = Configuration.defaultConfiguration().addOptions( DEFAULT_OPTIONS ); this.log = log; }
public Object replace(String paramString) { Configuration option = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS); DocumentContext documentContext = JsonPath.parse(Collections.emptyMap(), option); return replaceVariables(paramString, documentContext, null); }
public Map<String, Object> replace(Map<String, Object> input, Object json) { Object doc; if (json instanceof String) { doc = JsonPath.parse(json.toString()); } else { doc = json; } Configuration option = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS); DocumentContext documentContext = JsonPath.parse(doc, option); return replace(input, documentContext, null); }
@Test public void when_deep_scanning_require_properties_is_ignored_on_scan_target_but_not_on_children() { final Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); assertEvaluationThrows("{\"foo\": {\"baz\": 4}}", "$..foo.bar", PathNotFoundException.class, conf); }
@Test public void when_property_is_required_exception_is_thrown_2() { Map<String, Object> model = new HashMap<String, Object>(); model.put("a", singletonMap("a-key", "a-val")); model.put("b", singletonMap("b-key", "b-val")); Configuration conf = Configuration.defaultConfiguration(); assertThat(using(conf).parse(model).read("$.*.a-key", List.class)).containsExactly("a-val"); conf = conf.addOptions(Option.REQUIRE_PROPERTIES); try{ using(conf).parse(model).read("$.*.a-key", List.class); fail("Should throw PathNotFoundException"); } catch (PathNotFoundException pnf){} }
@Test public void require_single_property_ok() { List json = new ArrayList() {{ add(singletonMap("a", "a0")); add(singletonMap("a", "a1")); }}; Configuration configuration = JSON_SMART_CONFIGURATION.addOptions(Option.REQUIRE_PROPERTIES); Object result = JsonPath.using(configuration).parse(json).read("$..a"); assertThat(result).asList().containsExactly("a0","a1"); }
@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 when_property_is_required_exception_is_thrown() { List<Map<String, String>> model = asList(singletonMap("a", "a-val"),singletonMap("b", "b-val")); Configuration conf = Configuration.defaultConfiguration(); assertThat(using(conf).parse(model).read("$[*].a", List.class)).containsExactly("a-val"); conf = conf.addOptions(Option.REQUIRE_PROPERTIES); try{ using(conf).parse(model).read("$[*].a", List.class); fail("Should throw PathNotFoundException"); } catch (PathNotFoundException pnf){} }
@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 isbn_is_defaulted_when_option_is_provided() { List<String> result1 = JsonPath.using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.*.isbn"); assertThat(result1).containsExactly("0-553-21311-3","0-395-19395-8"); List<String> result2 = JsonPath.using(JSON_SMART_CONFIGURATION.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(JSON_DOCUMENT).read("$.store.book.*.isbn"); assertThat(result2).containsExactly(null, null, "0-553-21311-3", "0-395-19395-8"); } }
@Test public void non_leaf_multi_props_can_be_required() { final Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); final String json = "{\"a\": {\"v\": 5}, \"b\": {\"v\": 4}, \"c\": {\"v\": 1}}"; assertThat((List)using(conf).parse(json).read("$['a', 'c'].v")).asList().containsOnly(5, 1); assertEvaluationThrows(json, "$['d', 'a', 'c', 'm'].v", PathNotFoundException.class, conf); } }
@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 require_multi_property_all_match() { final Map ab = new HashMap(){{ put("a", "aa"); put("b", "bb"); }}; List json = new ArrayList() {{ add(ab); add(ab); }}; Configuration configuration = JSON_SMART_CONFIGURATION.addOptions(Option.REQUIRE_PROPERTIES); List<Map<String, String>> result = JsonPath.using(configuration).parse(json).read("$..['a', 'b']"); assertThat(result).containsExactly(ab, ab); }
@Test public void multi_props_can_be_defaulted_to_null() { 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.DEFAULT_PATH_LEAF_TO_NULL); assertThat(using(conf).parse(model).read("$['a', 'd']", Map.class)) .containsEntry("a", "a-val") .containsEntry("d", null); }
@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); }