Search in sources :

Example 46 with DocumentContext

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}}"));
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 47 with DocumentContext

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");
}
Also used : Filter(com.jayway.jsonpath.Filter) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 48 with DocumentContext

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);
}
Also used : JacksonJsonNodeJsonProvider(com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider) JacksonMappingProvider(com.jayway.jsonpath.spi.mapper.JacksonMappingProvider) Configuration(com.jayway.jsonpath.Configuration) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonPath(com.jayway.jsonpath.JsonPath) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 49 with DocumentContext

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");
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 50 with DocumentContext

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);
    }
}
Also used : Matcher(java.util.regex.Matcher) DocumentContext(com.jayway.jsonpath.DocumentContext) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) InvalidPathException(com.jayway.jsonpath.InvalidPathException) TableException(org.apache.flink.table.api.TableException) JsonProcessingException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)146 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)8 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)4