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;
}
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);
}
}
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();
}
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);
}
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);
}
Aggregations