Search in sources :

Example 6 with ValueSerializationException

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

the class StaxValueDeserializer method readMapInMap.

@Override
protected <K, V> Map<K, V> readMapInMap(XMLEventReader input, Function<XMLEventReader, K> keyDeserializer, Function<XMLEventReader, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (!input.hasNext()) {
        return null;
    }
    XMLEvent nextTag = input.nextTag();
    if (nextTag.isStartElement() && "null".equals(nextTag.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (!nextTag.isStartElement() || !"array".equals(nextTag.asStartElement().getName().getLocalPart())) {
        throw new ValueSerializationException("Expected an <array/> but got: " + nextTag);
    }
    // <object>
    XMLEvent currentTag = input.nextTag();
    while (!currentTag.isEndElement() || !"array".equals(currentTag.asEndElement().getName().getLocalPart())) {
        if (!currentTag.isStartElement() || !"object".equals(currentTag.asStartElement().getName().getLocalPart())) {
            throw new ValueSerializationException("Expected an <object/> but got: " + nextTag);
        }
        // <field>
        currentTag = input.nextTag();
        K key = null;
        V value = null;
        while (!currentTag.isEndElement() || !"object".equals(currentTag.asEndElement().getName().getLocalPart())) {
            // <name>
            input.nextTag();
            String keyOrValue = input.nextEvent().asCharacters().getData();
            // </name>
            input.nextTag();
            // <value>
            input.nextTag();
            switch(keyOrValue) {
                case "key":
                    key = keyDeserializer.map(input);
                    break;
                case "value":
                    value = valueDeserializer.map(input);
                    break;
                default:
                    readObjectTree(input);
                    break;
            }
            // </value>
            input.nextTag();
            // </field>
            input.nextTag();
            currentTag = input.nextTag();
        }
        if (key != null) {
            map.put(key, value);
        }
        currentTag = input.nextTag();
    }
    return map;
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 7 with ValueSerializationException

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

the class ValueCompositeRequestWriter method writeRequest.

@Override
public boolean writeRequest(Object requestObject, Request request) throws ResourceException {
    if (requestObject instanceof ValueComposite) {
        // Value as parameter
        final ValueComposite valueObject = (ValueComposite) requestObject;
        if (request.getMethod().equals(Method.GET)) {
            StateHolder holder = spi.stateOf(valueObject);
            final ValueDescriptor descriptor = spi.valueDescriptorFor(valueObject);
            final Reference ref = request.getResourceRef();
            ref.setQuery(null);
            try {
                for (PropertyDescriptor propertyDescriptor : descriptor.state().properties()) {
                    Object value = holder.propertyFor(propertyDescriptor.accessor()).get();
                    String param;
                    if (value == null) {
                        param = null;
                    } else {
                        param = valueSerializer.serialize(value);
                    }
                    ref.addQueryParameter(propertyDescriptor.qualifiedName().name(), param);
                }
            } catch (ValueSerializationException e) {
                throw new ResourceException(e);
            }
        } else {
            request.setEntity(new WriterRepresentation(MediaType.APPLICATION_JSON) {

                @Override
                public void write(Writer writer) throws IOException {
                    setCharacterSet(CharacterSet.UTF_8);
                    valueSerializer.serialize(valueObject, new WriterOutputStream(writer));
                }
            });
        }
        return true;
    }
    return false;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Reference(org.restlet.data.Reference) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) IOException(java.io.IOException) WriterOutputStream(org.restlet.engine.io.WriterOutputStream) ValueComposite(org.qi4j.api.value.ValueComposite) WriterRepresentation(org.restlet.representation.WriterRepresentation) StateHolder(org.qi4j.api.property.StateHolder) ResourceException(org.restlet.resource.ResourceException) RequestWriter(org.qi4j.library.rest.client.spi.RequestWriter) Writer(java.io.Writer)

Example 8 with ValueSerializationException

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

the class ModuleInstance method newValueFromSerializedState.

@Override
public <T> T newValueFromSerializedState(Class<T> mixinType, String serializedState) throws NoSuchValueException, ConstructionException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel(mixinType);
    if (modelModule == null) {
        throw new NoSuchValueException(mixinType.getName(), name());
    }
    try {
        return valueSerialization().deserialize(modelModule.model().valueType(), serializedState);
    } catch (ValueSerializationException ex) {
        throw new ConstructionException("Could not create value from serialized state", ex);
    }
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ConstructionException(org.qi4j.api.common.ConstructionException)

Example 9 with ValueSerializationException

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

the class ValueSerializerAdapter method serialize.

@Override
public final String serialize(Options options, Object object) throws ValueSerializationException {
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        serializeRoot(options, object, output);
        return output.toString(UTF_8);
    } catch (ValueSerializationException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ValueSerializationException("Could not serialize value", ex);
    }
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 10 with ValueSerializationException

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

the class OrgJsonValueDeserializer method putArrayNodeInCollection.

@Override
protected <T> void putArrayNodeInCollection(Object inputNode, Function<Object, T> deserializer, Collection<T> collection) 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);
        T value = deserializer.map(item);
        collection.add(value);
    }
}
Also used : JSONArray(org.json.JSONArray) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JSONObject(org.json.JSONObject)

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