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;
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations