Example usage for com.fasterxml.jackson.core ObjectCodec treeToValue

List of usage examples for com.fasterxml.jackson.core ObjectCodec treeToValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core ObjectCodec treeToValue.

Prototype

public abstract <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingException;

Source Link

Document

Convenience method for converting given JSON tree into instance of specified value type.

Usage

From source file:com.evrythng.java.wrapper.mapping.TypeMapDeserializer.java

@Override
public T deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {

    ObjectCodec codec = jp.getCodec();
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = mapper.readTree(jp);
    JsonNode type = root.get(typeFieldName);
    final String sType = type == null ? null : type.textValue();

    Class<? extends T> clazz = resolveClass(sType);

    return codec.treeToValue(root, clazz);
}

From source file:org.apache.unomi.persistence.spi.ItemDeserializer.java

@Override
public Item deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectCodec codec = jp.getCodec();
    ObjectNode treeNode = codec.readTree(jp);
    String type = treeNode.get("itemType").textValue();
    Class<? extends Item> objectClass = classes.get(type);
    if (objectClass == null) {
        objectClass = CustomItem.class;
    } else {//from  w ww  . j  a  va2 s.com
        treeNode.remove("itemType");
    }
    Item item = codec.treeToValue(treeNode, objectClass);
    item.setItemId(treeNode.get("itemId").asText());
    return item;
}

From source file:com.evrythng.java.wrapper.mapping.ActionDeserializerImpl.java

@Override
public Action deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {

    ObjectCodec codec = jp.getCodec();
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = mapper.readTree(jp);
    JsonNode type = root.get(getTypeFieldName());
    if (type == null) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }//  ww  w. j  a v  a  2s  . c o m
    String sType = type.textValue();
    if (sType == null || sType.isEmpty()) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }

    return codec.treeToValue(root, resolveClass(sType));
}

From source file:com.evrythng.java.wrapper.mapping.GeoJsonDeserializerImpl.java

@Override
public GeoJson deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectCodec codec = jp.getCodec();
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    JsonNode type = root.get(getTypeFieldName());
    if (type == null) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }/*w w  w  . j av a 2s  .c om*/
    String sType = type.textValue();
    if (sType == null || sType.isEmpty()) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }

    Class<GeoJson> clazz = (Class<GeoJson>) resolveClass(sType);

    GeoJson obj = codec.treeToValue(root, clazz);
    if (obj == null) {
        throw new IllegalArgumentException(
                this.getValueClass().getSimpleName() + " type deserialised as null: " + root.toString());
    }
    return obj;
}

From source file:org.apache.unomi.persistence.spi.PropertyTypedObjectDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp.getCurrentTokenId() != JsonTokenId.ID_START_OBJECT) {
        return super.deserialize(jp, ctxt);
    }/*from  www.j a v  a2s . co  m*/
    ObjectCodec codec = jp.getCodec();
    TreeNode treeNode = codec.readTree(jp);
    Class<? extends Object> objectClass = null;
    if (treeNode instanceof ObjectNode) {
        ObjectNode root = (ObjectNode) treeNode;
        Iterator<Map.Entry<String, JsonNode>> elementsIterator = root.fields();
        while (elementsIterator.hasNext()) {
            Map.Entry<String, JsonNode> element = elementsIterator.next();
            String name = element.getKey();
            if (fieldValuesToMatch.containsKey(name)) {
                Set<String> valuesToMatch = fieldValuesToMatch.get(name);
                for (String valueToMatch : valuesToMatch) {
                    if (element.getValue().asText().matches(valueToMatch)) {
                        objectClass = registry.get(name + "=" + valueToMatch);
                        break;
                    }
                }
                if (objectClass != null) {
                    break;
                }
            }
        }
        if (objectClass == null) {
            objectClass = HashMap.class;
        }
    } else {

    }
    if (objectClass == null) {
        return super.deserialize(codec.treeAsTokens(treeNode), ctxt);
    }
    return codec.treeToValue(treeNode, objectClass);
}