Search in sources :

Example 51 with JsonPath

use of com.jayway.jsonpath.JsonPath in project qpp-conversion-tool by CMSgov.

the class PathCorrelator method prepPath.

/**
 * Assemble an xpath using the given json path and json wrapper.
 *
 * @param jsonPath definite json path
 * @param wrapper object representation of QPP json
 * @return xpath that correlates to supplied json path
 */
public static String prepPath(String jsonPath, JsonWrapper wrapper) {
    String base = "$";
    String leaf = jsonPath;
    int lastIndex = jsonPath.lastIndexOf('.');
    JsonWrapper metaWrapper = new JsonWrapper(wrapper, false);
    if (lastIndex > 0) {
        base = jsonPath.substring(0, lastIndex);
        leaf = jsonPath.substring(lastIndex + 1);
    }
    JsonPath compiledPath = JsonPath.compile(base);
    Map<String, Object> jsonMap = compiledPath.read(metaWrapper.toString());
    Map<String, String> metaMap = getMetaMap(jsonMap, leaf);
    String preparedPath = "";
    if (metaMap != null) {
        preparedPath = makePath(metaMap, leaf);
    }
    return preparedPath;
}
Also used : JsonWrapper(gov.cms.qpp.conversion.encode.JsonWrapper) JsonPath(com.jayway.jsonpath.JsonPath)

Example 52 with JsonPath

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

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 53 with JsonPath

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

the class IssuesTest method github_89.

@Test(expected = PathNotFoundException.class)
public void github_89() {
    com.google.gson.JsonObject json = new JsonObject();
    json.addProperty("foo", "bar");
    JsonPath path = JsonPath.compile("$.foo");
    String object = path.read(json);
}
Also used : JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonPath(com.jayway.jsonpath.JsonPath) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 54 with JsonPath

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

the class IssuesTest method issue_94_2.

@Test
public void issue_94_2() throws Exception {
    LRUCache cache = new LRUCache(5);
    JsonPath dummy = JsonPath.compile("$");
    cache.put("1", dummy);
    cache.put("2", dummy);
    cache.put("3", dummy);
    cache.put("4", dummy);
    cache.put("5", dummy);
    cache.put("6", dummy);
    cache.get("1");
    cache.get("2");
    cache.get("3");
    cache.get("4");
    cache.get("5");
    cache.get("6");
    cache.get("2");
    cache.get("3");
    cache.get("4");
    cache.get("5");
    cache.get("6");
    cache.get("3");
    cache.get("4");
    cache.get("5");
    cache.get("6");
    cache.get("4");
    cache.get("5");
    cache.get("6");
    cache.get("5");
    cache.get("6");
    cache.get("6");
    assertThat(cache.getSilent("6")).isNotNull();
    assertThat(cache.getSilent("5")).isNotNull();
    assertThat(cache.getSilent("4")).isNotNull();
    assertThat(cache.getSilent("3")).isNotNull();
    assertThat(cache.getSilent("2")).isNotNull();
    assertThat(cache.getSilent("1")).isNull();
}
Also used : LRUCache(com.jayway.jsonpath.spi.cache.LRUCache) JsonPath(com.jayway.jsonpath.JsonPath) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 55 with JsonPath

use of com.jayway.jsonpath.JsonPath in project graylog2-server by Graylog2.

the class HTTPJSONPathDataAdapterTest method parseEmptyBody.

@Test
public void parseEmptyBody() throws Exception {
    final JsonPath singlePath = JsonPath.compile("$.hello");
    final JsonPath multiPath = JsonPath.compile("$.list");
    final LookupResult result = HTTPJSONPathDataAdapter.parseBody(singlePath, multiPath, emptyBody);
    assertThat(result).isNull();
}
Also used : LookupResult(org.graylog2.plugin.lookup.LookupResult) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

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