Search in sources :

Example 1 with ConfigurationBuilder

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));
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) ConfigurationBuilder(com.jayway.jsonpath.Configuration.ConfigurationBuilder) Configuration(com.jayway.jsonpath.Configuration) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) LinkedList(java.util.LinkedList) IntNode(com.fasterxml.jackson.databind.node.IntNode) Entry(java.util.Map.Entry) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 2 with ConfigurationBuilder

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;
}
Also used : ConfigurationBuilder(com.jayway.jsonpath.Configuration.ConfigurationBuilder) Configuration(com.jayway.jsonpath.Configuration)

Example 3 with ConfigurationBuilder

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();
}
Also used : ConfigurationBuilder(com.jayway.jsonpath.Configuration.ConfigurationBuilder) ParseContext(com.jayway.jsonpath.ParseContext) DocumentContext(com.jayway.jsonpath.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with ConfigurationBuilder

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());
}
Also used : ConfigurationBuilder(com.jayway.jsonpath.Configuration.ConfigurationBuilder) Configuration(com.jayway.jsonpath.Configuration) HashSet(java.util.HashSet)

Aggregations

ConfigurationBuilder (com.jayway.jsonpath.Configuration.ConfigurationBuilder)4 Configuration (com.jayway.jsonpath.Configuration)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 DocumentContext (com.jayway.jsonpath.DocumentContext)1 ParseContext (com.jayway.jsonpath.ParseContext)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 BatfishException (org.batfish.common.BatfishException)1 Test (org.junit.Test)1