/** * Creates a JSONAsserter * * @param json the JSON document to create a JSONAsserter for * @return a JSON asserter initialized with the provided document * @throws ParseException when the given JSON could not be parsed */ public static JsonAsserter with(String json) { return new JsonAsserterImpl(JsonPath.parse(json).json()); }
/** * Creates a JSONAsserter * * @param reader the reader of the json document * @return a JSON asserter initialized with the provided document * @throws ParseException when the given JSON could not be parsed */ public static JsonAsserter with(Reader reader) throws IOException { return new JsonAsserterImpl(JsonPath.parse(convertReaderToString(reader)).json()); }
@Test public void an_root_property_can_be_updated() { Object o = parse(JSON_DOCUMENT).set("$.int-max-property", 1).json(); Integer result = parse(o).read("$.int-max-property"); assertThat(result).isEqualTo(1); }
@Test public void an_array_can_be_updated() { List<Integer> ints = parse("[0,1,2,3]").set("$[?(@ == 1)]", 9).json(); assertThat(ints).containsExactly(0, 9, 2, 3); }
@Test(expected = InvalidModificationException.class) public void rootCannotBeMapped(){ MapFunction mapFunction = new MapFunction() { @Override public Object map(Object currentValue, Configuration configuration) { return currentValue.toString()+"converted"; } }; Object o = parse(JSON_DOCUMENT).map("$", mapFunction).json(); }
@Test(expected = PathNotFoundException.class) public void non_existent_key_rename_not_allowed(){ Object o = parse(JSON_DOCUMENT).renameKey("$", "fake", "new-fake").json(); }
@Test public void an_filter_can_update() { Object o = parse(JSON_DOCUMENT).set("$.store.book[?(@.display-price)].display-price", 1).json(); List<Integer> result = parse(o).read("$.store.book[?(@.display-price)].display-price"); assertThat(result).containsExactly(1, 1, 1, 1); }
@Test public void an_deep_scan_can_update() { Object o = parse(JSON_DOCUMENT).set("$..display-price", 1).json(); List<Integer> result = parse(o).read("$..display-price"); assertThat(result).containsExactly(1, 1, 1, 1, 1); }
@Test public void a_path_can_be_deleted() { Object o = parse(JSON_DOCUMENT).delete("$.store.book[*].display-price").json(); List<Integer> result = parse(o).read("$.store.book[*].display-price"); assertThat(result).isEmpty(); }
@Test public void a_path_can_be_renamed(){ Object o = parse(JSON_DOCUMENT).renameKey("$.store", "book", "updated-book").json(); List<Object> result = parse(o).read("$.store.updated-book"); assertThat(result).isNotEmpty(); }
@Test public void an_array_child_property_can_be_updated() { Object o = parse(JSON_DOCUMENT).set("$.store.book[*].display-price", 1).json(); List<Integer> result = parse(o).read("$.store.book[*].display-price"); assertThat(result).containsExactly(1, 1, 1, 1); }
@Test public void keys_in_root_containing_map_can_be_renamed(){ Object o = parse(JSON_DOCUMENT).renameKey("$", "store", "new-store").json(); List<Object> result = parse(o).read("$.new-store[*]"); assertThat(result).isNotEmpty(); }
@Test public void map_array_items_can_be_renamed(){ Object o = parse(JSON_DOCUMENT).renameKey("$.store.book[*]", "category", "renamed-category").json(); List<Object> result = parse(o).read("$.store.book[*].renamed-category"); assertThat(result).isNotEmpty(); }
@Test public void operations_can_chained() { Object o = parse(JSON_DOCUMENT) .delete("$.store.book[*].display-price") .set("$.store.book[*].category", "A") .json(); List<Integer> prices = parse(o).read("$.store.book[*].display-price"); List<String> categories = parse(o).read("$.store.book[*].category"); assertThat(prices).isEmpty(); assertThat(categories).containsExactly("A", "A", "A", "A"); }
@Test public void combine_filter_deep_criteria() { String json = "[\n" + " {\n" + " \"first-name\" : \"John\",\n" + " \"last-name\" : \"Irving\",\n" + " \"address\" : {\"state\" : \"Texas\"}\n" + " },\n" + " {\n" + " \"first-name\" : \"Jock\",\n" + " \"last-name\" : \"Ewing\",\n" + " \"address\" : {\"state\" : \"Texas\"}\n" + " },\n" + " {\n" + " \"first-name\" : \"Jock\",\n" + " \"last-name\" : \"Barnes\",\n" + " \"address\" : {\"state\" : \"Nevada\"}\n" + " } \n" + "]"; Filter filter = filter( where("first-name").is("Jock") .and("address.state").is("Texas")); List<Map<String, Object>> jocksInTexas1 = JsonPath.read(json, "$[?]", filter); List<Map<String, Object>> jocksInTexas2 = JsonPath.read(json, "$[?(@.first-name == 'Jock' && @.address.state == 'Texas')]"); JsonPath.parse(json).json(); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].address.state"), is("Texas")); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].first-name"), is("Jock")); assertThat((String)JsonPath.read(jocksInTexas1, "$[0].last-name"), is("Ewing")); }
private Json(DocumentContext doc) { this.doc = doc; array = (doc.json() instanceof List); prefix = array ? "$" : "$."; }
public boolean isListLike() { switch (type) { case JS_ARRAY: case LIST: return true; case JSON: DocumentContext doc = (DocumentContext) value; return doc.json() instanceof List; default: return false; } }
public static <T> T readValue(byte[] bytes, Class<T> type) throws IOException { DocumentContext document = JsonPath.using(conf).parse(bytes); if (type.isInstance(DocumentContext.class)) { return (T) document; } return document.json(); }
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()); }
private static JsonPathResult computeJsonPathResult( 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 result; }