use of com.jayway.jsonpath.DocumentContext in project JsonPath by jayway.
the class Issue_721 method test_delete_1.
@Test
public void test_delete_1() {
// originally throws PathNotFoundException
DocumentContext dc = JsonPath.using(jsonConf).parse("{\"top\": {\"middle\": null}}").delete(JsonPath.compile("$.top.middle.bottom"));
Object ans = dc.read("$");
// System.out.println(ans);
assert (ans.toString().equals("{top={middle=null}}"));
}
use of com.jayway.jsonpath.DocumentContext in project JsonPath by jayway.
the class JsonContextTest method cached_path_with_predicates.
@Test
public void cached_path_with_predicates() {
Filter feq = Filter.filter(Criteria.where("category").eq("reference"));
Filter fne = Filter.filter(Criteria.where("category").ne("reference"));
DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT);
List<String> eq = JsonDoc.read("$.store.book[?].category", feq);
List<String> ne = JsonDoc.read("$.store.book[?].category", fne);
Assertions.assertThat(eq).contains("reference");
Assertions.assertThat(ne).doesNotContain("reference");
}
use of com.jayway.jsonpath.DocumentContext in project JsonPath by jayway.
the class IssuesTest method issue_378.
@Test
public void issue_378() {
String json = "{\n" + " \"nodes\": {\n" + " \"unnamed1\": {\n" + " \"ntpServers\": [\n" + " \"1.2.3.4\"\n" + " ]\n" + " }\n" + " }\n" + "}";
Configuration configuration = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build();
DocumentContext ctx = JsonPath.using(configuration).parse(json);
String path = "$.nodes[*][?(!([\"1.2.3.4\"] subsetof @.ntpServers))].ntpServers";
JsonPath jsonPath = JsonPath.compile(path);
ctx.read(jsonPath);
}
use of com.jayway.jsonpath.DocumentContext in project JsonPath by jayway.
the class IssuesTest method issue_309.
@Test
public void issue_309() {
String json = "{\n" + "\"jsonArr\": [\n" + " {\n" + " \"name\":\"nOne\"\n" + " },\n" + " {\n" + " \"name\":\"nTwo\"\n" + " }\n" + " ]\n" + "}";
DocumentContext doc = JsonPath.parse(json).set("$.jsonArr[1].name", "Jayway");
assertThat((String) doc.read("$.jsonArr[0].name")).isEqualTo("nOne");
assertThat((String) doc.read("$.jsonArr[1].name")).isEqualTo("Jayway");
}
use of com.jayway.jsonpath.DocumentContext in project flink by apache.
the class SqlJsonUtils method jsonApiCommonSyntax.
private static JsonPathContext jsonApiCommonSyntax(JsonValueContext input, String pathSpec) {
PathMode mode;
String pathStr;
try {
Matcher matcher = JSON_PATH_BASE.matcher(pathSpec);
if (!matcher.matches()) {
mode = PathMode.STRICT;
pathStr = pathSpec;
} else {
mode = PathMode.valueOf(matcher.group(1).toUpperCase(Locale.ROOT));
pathStr = matcher.group(2);
}
DocumentContext ctx;
switch(mode) {
case STRICT:
if (input.hasException()) {
return JsonPathContext.withStrictException(pathSpec, input.exc);
}
ctx = JsonPath.parse(input.obj, Configuration.builder().jsonProvider(JSON_PATH_JSON_PROVIDER).mappingProvider(JSON_PATH_MAPPING_PROVIDER).build());
break;
case LAX:
if (input.hasException()) {
return JsonPathContext.withJavaObj(PathMode.LAX, null);
}
ctx = JsonPath.parse(input.obj, Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).jsonProvider(JSON_PATH_JSON_PROVIDER).mappingProvider(JSON_PATH_MAPPING_PROVIDER).build());
break;
default:
throw illegalJsonPathModeInPathSpec(mode.toString(), pathSpec);
}
try {
return JsonPathContext.withJavaObj(mode, ctx.read(pathStr));
} catch (Exception e) {
return JsonPathContext.withStrictException(pathSpec, e);
}
} catch (Exception e) {
return JsonPathContext.withUnknownException(e);
}
}
Aggregations