Search in sources :

Example 1 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class ValueDeserializerAdapter method deserializeNodeValueComposite.

private <T> T deserializeNodeValueComposite(ValueType valueType, InputNodeType inputNode) throws Exception {
    ValueCompositeType valueCompositeType = (ValueCompositeType) valueType;
    Class<?> valueBuilderType = first(valueCompositeType.types());
    String typeInfo = this.<String>getObjectFieldValue(inputNode, "_type", this.<String>buildDeserializeInputNodeFunction(new ValueType(String.class)));
    TREE_PARSING_LOG.trace("In deserializeNodeValueComposite(), getObjectFieldValue( {} ) returned '{}'", inputNode, typeInfo);
    if (typeInfo != null) {
        ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(typeInfo);
        if (valueDescriptor == null) {
            throw new ValueSerializationException("Specified value type could not be resolved: " + typeInfo);
        }
        valueCompositeType = valueDescriptor.valueType();
        valueBuilderType = Class.forName(typeInfo);
        if (!valueType.equals(valueCompositeType)) {
            TREE_PARSING_LOG.debug("Overriding {} with {} as defined in _type field.", valueType, valueCompositeType);
        }
    }
    return deserializeValueComposite(valueCompositeType, valueBuilderType, inputNode);
}
Also used : ValueType(org.qi4j.api.type.ValueType) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 2 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class ValueDeserializerAdapter method deserializeNodeGuessed.

@SuppressWarnings("unchecked")
private <T> T deserializeNodeGuessed(ValueType valueType, InputNodeType inputNode) throws Exception {
    if (isObjectValue(inputNode)) {
        // Attempt ValueComposite deserialization
        ValueCompositeType valueCompositeType;
        if (// with _type info
        objectHasField(inputNode, "_type")) {
            String typeInfo = this.<String>getObjectFieldValue(inputNode, "_type", this.<String>buildDeserializeInputNodeFunction(new ValueType(String.class)));
            TREE_PARSING_LOG.trace("In deserializeNodeGuessed(), getObjectFieldValue( {} ) returned '{}'", inputNode, typeInfo);
            ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(typeInfo);
            if (valueDescriptor == null) {
                throw new ValueSerializationException("Specified value type could not be resolved: " + typeInfo);
            }
            valueCompositeType = valueDescriptor.valueType();
            TREE_PARSING_LOG.debug("Overriding {} with {} as defined in _type field.", valueType, valueCompositeType);
        } else // without _type info
        {
            ValueDescriptor valueDescriptor = valuesModule().valueDescriptor(first(valueType.types()).getName());
            if (valueDescriptor == null) {
                throw new ValueSerializationException("Don't know how to deserialize " + inputNode);
            }
            valueCompositeType = valueDescriptor.valueType();
            TREE_PARSING_LOG.debug("Overriding {} with {} as found in available ValueComposites.", valueType, valueCompositeType);
        }
        Class<?> valueBuilderType = first(valueCompositeType.types());
        return deserializeValueComposite(valueCompositeType, valueBuilderType, inputNode);
    }
    // Last resort : base64 java deserialization
    return (T) deserializeBase64Serialized(inputNode);
}
Also used : ValueType(org.qi4j.api.type.ValueType) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 3 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class OrgJsonValueDeserializer method putArrayNodeInMap.

@Override
protected <K, V> void putArrayNodeInMap(Object inputNode, Function<Object, K> keyDeserializer, Function<Object, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (JSONObject.NULL.equals(inputNode)) {
        return;
    }
    if (!(inputNode instanceof JSONArray)) {
        throw new ValueSerializationException("Expected an array but got " + inputNode);
    }
    JSONArray array = (JSONArray) inputNode;
    for (int idx = 0; idx < array.length(); idx++) {
        Object item = array.get(idx);
        if (!(item instanceof JSONObject)) {
            throw new ValueSerializationException("Expected an object but got " + inputNode);
        }
        JSONObject object = (JSONObject) item;
        Object keyNode = object.get("key");
        Object valueNode = object.get("value");
        K key = keyDeserializer.map(keyNode);
        V value = valueDeserializer.map(valueNode);
        if (key != null) {
            map.put(key, value);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JSONObject(org.json.JSONObject)

Example 4 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method putArrayNodeInMap.

@Override
protected <K, V> void putArrayNodeInMap(Node inputNode, Function<Node, K> keyDeserializer, Function<Node, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (inputNode == null) {
        return;
    }
    if (!"array".equals(inputNode.getLocalName())) {
        throw new ValueSerializationException("Expected an <array/> but got " + inputNode);
    }
    NodeList entriesNodes = inputNode.getChildNodes();
    for (int idx = 0; idx < entriesNodes.getLength(); idx++) {
        Node entryNode = entriesNodes.item(idx);
        K key = getObjectFieldValue(entryNode, "key", keyDeserializer);
        V value = getObjectFieldValue(entryNode, "value", valueDeserializer);
        if (key != null) {
            map.put(key, value);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 5 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method getObjectFieldNode.

private static Node getObjectFieldNode(Node inputNode, String key) {
    if (inputNode == null) {
        return null;
    }
    if (!(inputNode instanceof Element)) {
        throw new ValueSerializationException("Excpected an Element but got " + inputNode);
    }
    NodeList fieldNodes = inputNode.getChildNodes();
    for (int idx = 0; idx < fieldNodes.getLength(); idx++) {
        Node fieldNode = fieldNodes.item(idx);
        Node nameNode = getDirectChildNode(fieldNode, "name");
        if (nameNode != null && key.equals(nameNode.getTextContent())) {
            return fieldNode;
        }
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Aggregations

ValueSerializationException (org.qi4j.api.value.ValueSerializationException)25 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)6 ValueType (org.qi4j.api.type.ValueType)5 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)5 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 Node (org.w3c.dom.Node)4 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 EntityReference (org.qi4j.api.entity.EntityReference)3 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Text (com.google.appengine.api.datastore.Text)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2