Search in sources :

Example 6 with JsonPath

use of com.jayway.jsonpath.JsonPath in project druid by druid-io.

the class JSONPathParser method generateFieldPaths.

private Map<String, Pair<FieldType, JsonPath>> generateFieldPaths(List<FieldSpec> fieldSpecs) {
    Map<String, Pair<FieldType, JsonPath>> map = new LinkedHashMap<>();
    for (FieldSpec fieldSpec : fieldSpecs) {
        String fieldName = fieldSpec.getName();
        if (map.get(fieldName) != null) {
            throw new IllegalArgumentException("Cannot have duplicate field definition: " + fieldName);
        }
        JsonPath path = fieldSpec.getType() == FieldType.PATH ? JsonPath.compile(fieldSpec.getExpr()) : null;
        Pair<FieldType, JsonPath> pair = new Pair<>(fieldSpec.getType(), path);
        map.put(fieldName, pair);
    }
    return map;
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) LinkedHashMap(java.util.LinkedHashMap) Pair(io.druid.java.util.common.Pair)

Example 7 with JsonPath

use of com.jayway.jsonpath.JsonPath in project druid by druid-io.

the class JSONPathParser method parse.

/**
   * @param input JSON string. The root must be a JSON object, not an array.
   *              e.g., {"valid": "true"} and {"valid":[1,2,3]} are supported
   *              but [{"invalid": "true"}] and [1,2,3] are not.
   *
   * @return A map of field names and values
   */
@Override
public Map<String, Object> parse(String input) {
    try {
        Map<String, Object> map = new LinkedHashMap<>();
        Map<String, Object> document = mapper.readValue(input, new TypeReference<Map<String, Object>>() {
        });
        for (Map.Entry<String, Pair<FieldType, JsonPath>> entry : fieldPathMap.entrySet()) {
            String fieldName = entry.getKey();
            Pair<FieldType, JsonPath> pair = entry.getValue();
            JsonPath path = pair.rhs;
            Object parsedVal;
            if (pair.lhs == FieldType.ROOT) {
                parsedVal = document.get(fieldName);
            } else {
                parsedVal = path.read(document, jsonPathConfig);
            }
            if (parsedVal == null) {
                continue;
            }
            parsedVal = valueConversionFunction(parsedVal);
            map.put(fieldName, parsedVal);
        }
        if (useFieldDiscovery) {
            discoverFields(map, document);
        }
        return map;
    } catch (Exception e) {
        throw new ParseException(e, "Unable to parse row [%s]", input);
    }
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(io.druid.java.util.common.Pair)

Example 8 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 9 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 10 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)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)15 BaseTest (com.jayway.jsonpath.BaseTest)5 Test (org.junit.Test)5 InvalidPathException (com.jayway.jsonpath.InvalidPathException)2 LRUCache (com.jayway.jsonpath.spi.cache.LRUCache)2 Pair (io.druid.java.util.common.Pair)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 JsonObject (com.google.gson.JsonObject)1 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 Cache (com.jayway.jsonpath.spi.cache.Cache)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 JSONObject (net.minidev.json.JSONObject)1 Expression (org.apache.camel.Expression)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1