Search in sources :

Example 16 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project Gaffer by gchq.

the class RoaringBitmapJsonDeserialiser method deserialize.

@Override
public RoaringBitmap deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
    final TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
    final TreeNode bitmapObject = treeNode.get(RoaringBitmapConstants.BITMAP_WRAPPER_OBJECT_NAME);
    if (null != bitmapObject) {
        final TextNode jsonNodes = (TextNode) bitmapObject.get(RoaringBitmapConstants.BITMAP_VALUE_FIELD_NAME);
        return (RoaringBitmap) bitmapSerialiser.deserialise(jsonNodes.binaryValue());
    } else {
        throw new IllegalArgumentException("Received null bitmap treenode");
    }
}
Also used : TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Example 17 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project Gaffer by gchq.

the class HllSketchJsonDeserialiser method deserialize.

@Override
public HllSketch deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
    final HllSketch hllp;
    final TextNode jsonNodes = (TextNode) treeNode.get(HllSketchJsonConstants.BYTES);
    if (isNull(jsonNodes)) {
        final IntNode kNode = (IntNode) treeNode.get(HllSketchJsonConstants.LOG_K);
        final int k = nonNull(kNode) ? kNode.asInt(DEFAULT_LOG_K) : DEFAULT_LOG_K;
        hllp = new HllSketch(k);
    } else {
        hllp = HllSketch.heapify(jsonNodes.binaryValue());
    }
    final ArrayNode offers = (ArrayNode) treeNode.get(VALUES);
    if (nonNull(offers)) {
        for (final JsonNode offer : offers) {
            if (nonNull(offer)) {
                hllp.update(offer.asText());
            }
        }
    }
    return hllp;
}
Also used : HllSketch(com.yahoo.sketches.hll.HllSketch) IntNode(com.fasterxml.jackson.databind.node.IntNode) 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 18 with TreeNode

use of com.fasterxml.jackson.core.TreeNode 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 19 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project snow-owl by b2ihealthcare.

the class ConceptPropertyDeserializer method deserialize.

@Override
public ConceptProperty<?> deserialize(JsonParser parser, DeserializationContext arg1) throws IOException, JsonProcessingException {
    TreeNode node = parser.readValueAsTree();
    ObjectCodec objectCodec = parser.getCodec();
    Iterator<String> fieldNames = node.fieldNames();
    PropertyType[] propertyTypes = PropertyType.values();
    PropertyType propertyType = null;
    while (fieldNames.hasNext()) {
        String fieldName = (String) fieldNames.next();
        if (fieldName.startsWith(VALUE_PREFIX)) {
            String type = fieldName.replace(VALUE_PREFIX, "");
            propertyType = Sets.newHashSet(propertyTypes).stream().filter(t -> t.getDisplayName().equalsIgnoreCase(type)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown property type '" + fieldName + "'."));
            break;
        }
    }
    if (propertyType == null) {
        throw new IllegalArgumentException("Invalid property type with null value.");
    }
    switch(propertyType) {
        case CODING:
            return objectCodec.treeToValue(node, CodingConceptProperty.class);
        case CODE:
            return objectCodec.treeToValue(node, CodeConceptProperty.class);
        case DATETIME:
            return objectCodec.treeToValue(node, DateTimeConceptProperty.class);
        case STRING:
            return objectCodec.treeToValue(node, StringConceptProperty.class);
        case BOOLEAN:
            return objectCodec.treeToValue(node, BooleanConceptProperty.class);
        case DECIMAL:
            return objectCodec.treeToValue(node, DecimalConceptProperty.class);
        case INTEGER:
            return objectCodec.treeToValue(node, IntegerConceptProperty.class);
        default:
            throw new IllegalArgumentException("Unsupported property type '" + propertyType + "'.");
    }
}
Also used : StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeNode(com.fasterxml.jackson.core.TreeNode) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) PropertyType(com.b2international.snowowl.fhir.core.codesystems.PropertyType) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) PropertyType(com.b2international.snowowl.fhir.core.codesystems.PropertyType)

Example 20 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project snow-owl by b2ihealthcare.

the class ExtensionDeserializer method deserialize.

@Override
public Extension<?> deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
    TreeNode node = parser.readValueAsTree();
    ObjectCodec objectCodec = parser.getCodec();
    Iterator<String> fieldNames = node.fieldNames();
    ExtensionType[] extensionTypes = ExtensionType.values();
    ExtensionType extensionType = null;
    while (fieldNames.hasNext()) {
        String fieldName = (String) fieldNames.next();
        if (fieldName.startsWith(VALUE_PREFIX)) {
            String type = fieldName.replace(VALUE_PREFIX, "");
            extensionType = Sets.newHashSet(extensionTypes).stream().filter(t -> t.getDisplayName().equalsIgnoreCase(type)).findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown extension type '" + fieldName + "'."));
            break;
        }
    }
    if (extensionType == null) {
        throw new IllegalArgumentException("Invalid extension with null value type.");
    }
    switch(extensionType) {
        case INTEGER:
            return objectCodec.treeToValue(node, IntegerExtension.class);
        case STRING:
            return objectCodec.treeToValue(node, StringExtension.class);
        default:
            throw new IllegalArgumentException("Unsupported extension type '" + extensionType + "'.");
    }
}
Also used : ExtensionType(com.b2international.snowowl.fhir.core.codesystems.ExtensionType) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) TreeNode(com.fasterxml.jackson.core.TreeNode) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Iterator(java.util.Iterator) JsonParser(com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) ExtensionType(com.b2international.snowowl.fhir.core.codesystems.ExtensionType) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec)

Aggregations

TreeNode (com.fasterxml.jackson.core.TreeNode)34 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 JsonParser (com.fasterxml.jackson.core.JsonParser)10 IOException (java.io.IOException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)4 BsonParser (de.undercouch.bson4jackson.BsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)3 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 Sets (com.google.common.collect.Sets)3 Iterator (java.util.Iterator)3 Test (org.junit.Test)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2