use of com.jayway.jsonpath.Configuration.ConfigurationBuilder in project batfish by batfish.
the class JsonPathResult method extractValuesFromSuffix.
private void extractValuesFromSuffix(String displayVar, Extraction extraction, JsonPathExtractionHint jpeHint) {
for (Entry<String, JsonPathResultEntry> entry : _result.entrySet()) {
if (!_displayValues.containsKey(entry.getKey())) {
_displayValues.put(entry.getKey(), new HashMap<>());
}
if (entry.getValue().getSuffix() == null) {
throw new BatfishException("Cannot compute suffix-based display values with null suffix. " + "(Was suffix set to True in the original JsonPath Query?)");
}
Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE);
Configuration c = (new ConfigurationBuilder()).build();
Object jsonObject = JsonPath.parse(entry.getValue().getSuffix(), c).json();
JsonPathQuery query = new JsonPathQuery(jpeHint.getFilter(), true);
List<JsonNode> extractedList = new LinkedList<>();
switch(jpeHint.getUse()) {
case FUNCOFSUFFIX:
{
if (!extraction.getSchemaAsObject().isIntOrIntList()) {
throw new BatfishException("schema must be INT(LIST) with funcofsuffix-based extraction hint");
}
Object result = JsonPathAnswerer.computePathFunction(jsonObject, query);
if (result != null) {
if (result instanceof Integer) {
extractedList.add(new IntNode((Integer) result));
} else if (result instanceof ArrayNode) {
for (JsonNode node : (ArrayNode) result) {
if (!(node instanceof IntNode)) {
throw new BatfishException("Got non-integer result from path function after filter " + query.getPath());
}
extractedList.add(node);
}
} else {
throw new BatfishException("Unknown result type from computePathFunction");
}
}
}
break;
case PREFIXOFSUFFIX:
case SUFFIXOFSUFFIX:
{
JsonPathResult filterResult = JsonPathAnswerer.computeResult(jsonObject, query);
Map<String, JsonPathResultEntry> filterResultEntries = filterResult.getResult();
for (Entry<String, JsonPathResultEntry> resultEntry : filterResultEntries.entrySet()) {
JsonNode value = (jpeHint.getUse() == UseType.PREFIXOFSUFFIX) ? new TextNode(resultEntry.getValue().getPrefixPart(jpeHint.getIndex())) : resultEntry.getValue().getSuffix();
confirmValueType(value, extraction.getSchemaAsObject().getBaseType());
extractedList.add(value);
}
}
break;
default:
throw new BatfishException("Unknown UseType " + jpeHint.getUse());
}
if (extractedList.size() == 0) {
throw new BatfishException("Got no results after filtering suffix values of the answer" + "\nFilter: " + jpeHint.getFilter() + "\nJson: " + jsonObject);
}
if (extraction.getSchemaAsObject().isList()) {
ArrayNode arrayNode = BatfishObjectMapper.mapper().valueToTree(extractedList);
_displayValues.get(entry.getKey()).put(displayVar, arrayNode);
} else {
if (extractedList.size() > 1) {
throw new BatfishException("Got multiple results after filtering suffix values " + " of the answer, but the display type is non-list");
}
_displayValues.get(entry.getKey()).put(displayVar, extractedList.get(0));
}
}
}
use of com.jayway.jsonpath.Configuration.ConfigurationBuilder in project batfish by batfish.
the class JsonPathDisplayHintsTest method computeJsonPathResult.
private static JsonPathResult computeJsonPathResult(String jsonFile, String path, boolean includeSuffix) {
Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE);
ConfigurationBuilder b = new ConfigurationBuilder();
Configuration baseConfiguration = b.build();
String jsonStr = CommonUtil.readResource(jsonFile);
Object jsonObject = JsonPath.parse(jsonStr, baseConfiguration).json();
JsonPathQuery query = new JsonPathQuery(path, includeSuffix);
JsonPathResult result = JsonPathAnswerer.computeResult(jsonObject, query);
return result;
}
use of com.jayway.jsonpath.Configuration.ConfigurationBuilder in project spring-data-commons by spring-projects.
the class ProjectionIntegrationTests method jacksonSerializationDoesNotExposeDecoratedClass.
// DATACMNS-909
@Test
public void jacksonSerializationDoesNotExposeDecoratedClass() throws Exception {
ProxyProjectionFactory factory = new ProxyProjectionFactory();
SampleProjection projection = factory.createProjection(SampleProjection.class);
ParseContext context = JsonPath.using(new ConfigurationBuilder().options(Option.SUPPRESS_EXCEPTIONS).build());
DocumentContext json = context.parse(new ObjectMapper().writeValueAsString(projection));
assertThat(json.read("$.decoratedClass", String.class)).isNull();
}
use of com.jayway.jsonpath.Configuration.ConfigurationBuilder in project batfish by batfish.
the class JsonPathAssertionTest method computeResults.
private static Set<JsonPathResultEntry> computeResults(String jsonFile, String path, boolean includeSuffix) {
Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE);
ConfigurationBuilder b = new ConfigurationBuilder();
Configuration baseConfiguration = b.build();
String jsonStr = CommonUtil.readResource(jsonFile);
Object jsonObject = JsonPath.parse(jsonStr, baseConfiguration).json();
JsonPathQuery query = new JsonPathQuery(path, includeSuffix);
JsonPathResult result = JsonPathAnswerer.computeResult(jsonObject, query);
return new HashSet<>(result.getResult().values());
}
Aggregations