Search in sources :

Example 46 with JsonPath

use of com.jayway.jsonpath.JsonPath in project JsonPath by json-path.

the class ApiResource method validate.

@GET
@Path("/validate")
@Produces(MediaType.APPLICATION_JSON)
public Response validate(@QueryParam("path") String path) {
    int result = -1;
    try {
        JsonPath compiled = JsonPath.compile(path);
        result = compiled.isDefinite() ? 0 : 1;
    } catch (Exception e) {
    }
    return Response.ok(Collections.singletonMap("result", result)).build();
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Path(javax.ws.rs.Path) JsonPath(com.jayway.jsonpath.JsonPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 47 with JsonPath

use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.

the class JsonPathTest method read_store_book_1.

@Test
public void read_store_book_1() throws Exception {
    JsonPath path = JsonPath.compile("$.store.book[1]");
    Map map = path.read(DOCUMENT);
    assertEquals("Evelyn Waugh", map.get("author"));
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Map(java.util.Map) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 48 with JsonPath

use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.

the class IssuesTest method issue_94_1.

@Test
public void issue_94_1() throws Exception {
    LRUCache cache = new LRUCache(200);
    JsonPath dummy = JsonPath.compile("$");
    for (int i = 0; i < 1000; ++i) {
        String key = String.valueOf(i);
        cache.get(key);
        cache.put(key, dummy);
    }
    assertThat(cache.size()).isEqualTo(200);
}
Also used : LRUCache(com.jayway.jsonpath.spi.cache.LRUCache) JsonPath(com.jayway.jsonpath.JsonPath) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 49 with JsonPath

use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.

the class Issue629 method testUncloseParenthesis.

@Test
public void testUncloseParenthesis() throws IOException {
    try {
        JsonPath jsonPath = JsonPath.compile("$.A.B.C.D(");
        fail("accepted jsonpath with unclosed parentheses");
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Arguments to function:"));
    }
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) IOException(java.io.IOException) Test(org.junit.Test)

Example 50 with JsonPath

use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.

the class JsonContext method pathFromCache.

private JsonPath pathFromCache(String path, Predicate[] filters) {
    Cache cache = CacheProvider.getCache();
    String cacheKey = filters == null || filters.length == 0 ? path : Utils.concat(path, Arrays.toString(filters));
    JsonPath jsonPath = cache.get(cacheKey);
    if (jsonPath == null) {
        jsonPath = compile(path, filters);
        cache.put(cacheKey, jsonPath);
    }
    return jsonPath;
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Cache(com.jayway.jsonpath.spi.cache.Cache)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)58 Test (org.junit.Test)34 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)13 BaseTest (com.jayway.jsonpath.BaseTest)12 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)9 ArrayList (java.util.ArrayList)9 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 Map (java.util.Map)8 DocumentContext (com.jayway.jsonpath.DocumentContext)6 IOException (java.io.IOException)6 ComponentLog (org.apache.nifi.logging.ComponentLog)6 File (java.io.File)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 DataType (org.apache.nifi.serialization.record.DataType)5 RecordField (org.apache.nifi.serialization.record.RecordField)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5