Search in sources :

Example 46 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project Gaffer by gchq.

the class HyperLogLogPlusJsonDeserialiser method deserialize.

@Override
public HyperLogLogPlus deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
    final TreeNode nestedHllp = treeNode.get("hyperLogLogPlus");
    if (nonNull(nestedHllp)) {
        treeNode = nestedHllp;
    }
    final HyperLogLogPlus hllp;
    final TextNode jsonNodes = (TextNode) treeNode.get(HyperLogLogPlusJsonConstants.HYPER_LOG_LOG_PLUS_SKETCH_BYTES_FIELD);
    if (isNull(jsonNodes)) {
        final IntNode pNode = (IntNode) treeNode.get("p");
        final IntNode spNode = (IntNode) treeNode.get("sp");
        final int p = nonNull(pNode) ? pNode.asInt(DEFAULT_P) : DEFAULT_P;
        final int sp = nonNull(spNode) ? spNode.asInt(DEFAULT_SP) : DEFAULT_SP;
        hllp = new HyperLogLogPlus(p, sp);
    } else {
        hllp = HyperLogLogPlus.Builder.build(jsonNodes.binaryValue());
    }
    final ArrayNode offers = (ArrayNode) treeNode.get("offers");
    if (nonNull(offers)) {
        for (final JsonNode offer : offers) {
            if (nonNull(offer)) {
                hllp.offer(offer.asText());
            }
        }
    }
    return hllp;
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 47 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode 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 48 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project snow-owl by b2ihealthcare.

the class TypedPropertyDeserializer method deserialize.

@Override
public TypedProperty deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
    String type = null;
    String currentName = parser.currentName();
    if (currentName.startsWith(VALUE_PREFIX)) {
        type = currentName.replace(VALUE_PREFIX, "");
    }
    if (type == null) {
        throw new IllegalArgumentException("Invalid parameter type with null value.");
    }
    TextNode node = (TextNode) parser.readValueAsTree();
    String value = node.asText();
    switch(type) {
        case "String":
            return new StringProperty(value);
        case "Date":
            return new DateProperty(FhirDates.parseDate(value));
        case "DateTime":
            return new DateTimeProperty(FhirDates.parseDate(value));
        case "Instant":
            return new InstantProperty(Instant.builder().instant(FhirDates.parseDate(value)).build());
        default:
            throw new IllegalArgumentException("Unsupported property type '" + type + "'.");
    }
}
Also used : TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 49 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project drools by kiegroup.

the class AbstractExpressionEvaluatorTest method isNodeEmpty.

@Test
public void isNodeEmpty() {
    ObjectNode objectNode = new ObjectNode(factory);
    assertTrue(expressionEvaluatorLocal.isNodeEmpty(objectNode));
    objectNode.set("empty array", new ArrayNode(factory));
    assertTrue(expressionEvaluatorLocal.isNodeEmpty(objectNode));
    objectNode.set("key", new TextNode(VALUE));
    assertFalse(expressionEvaluatorLocal.isNodeEmpty(objectNode));
    ArrayNode arrayNode = new ArrayNode(factory);
    assertTrue(expressionEvaluatorLocal.isNodeEmpty(arrayNode));
    arrayNode.add(new TextNode(VALUE));
    assertFalse(expressionEvaluatorLocal.isNodeEmpty(arrayNode));
    assertTrue(expressionEvaluatorLocal.isNodeEmpty(new TextNode("")));
    assertTrue(expressionEvaluatorLocal.isNodeEmpty(new TextNode(null)));
    assertFalse(expressionEvaluatorLocal.isNodeEmpty(new TextNode(VALUE)));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.Test)

Example 50 with TextNode

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode in project drools by kiegroup.

the class AbstractExpressionEvaluatorTest method isListEmpty.

@Test
public void isListEmpty() {
    ArrayNode json = new ArrayNode(factory);
    assertTrue(expressionEvaluatorLocal.isListEmpty(json));
    ObjectNode nestedNode = new ObjectNode(factory);
    json.add(nestedNode);
    assertTrue(expressionEvaluatorLocal.isListEmpty(json));
    nestedNode.set("emptyField", new TextNode(""));
    assertTrue(expressionEvaluatorLocal.isListEmpty(json));
    nestedNode.set("notEmptyField", new TextNode("text"));
    assertFalse(expressionEvaluatorLocal.isListEmpty(json));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Test(org.junit.Test)

Aggregations

TextNode (com.fasterxml.jackson.databind.node.TextNode)140 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)48 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)37 Test (org.junit.Test)37 JsonNode (com.fasterxml.jackson.databind.JsonNode)34 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 IntNode (com.fasterxml.jackson.databind.node.IntNode)16 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)15 MockScheduler (org.apache.kafka.common.utils.MockScheduler)14 MockTime (org.apache.kafka.common.utils.MockTime)14 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)14 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)13 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)12 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)12 TreeNode (com.fasterxml.jackson.core.TreeNode)11 HashMap (java.util.HashMap)11 List (java.util.List)11 NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)11 Map (java.util.Map)10