use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class GaeEntityState method setPropertyValue.
@Override
public void setPropertyValue(QualifiedName stateName, Object newValue) {
System.out.println("setProperty( " + stateName + ", " + newValue + " )");
Object value = null;
if (newValue == null || ValueType.isPrimitiveValue(newValue)) {
value = newValue;
} else {
try {
value = valueSerialization.serialize(newValue);
} catch (ValueSerializationException e) {
String message = "\nqualifiedName: " + stateName + "\n stateName: " + stateName.name() + "\n class: " + newValue.getClass() + "\n value: " + value + "\n";
InternalError error = new InternalError(message);
error.initCause(e);
throw error;
}
}
if (value instanceof String) {
value = new Text((String) value);
}
entity.setUnindexedProperty(stateName.toURI(), value);
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class PreferencesEntityStoreMixin method writeEntityState.
protected void writeEntityState(DefaultEntityState state, Preferences entityPrefs, String identity, long lastModified) throws EntityStoreException {
try {
// Store into Preferences API
entityPrefs.put("type", first(state.entityDescriptor().types()).getName());
entityPrefs.put("version", identity);
entityPrefs.putLong("modified", lastModified);
// Properties
Preferences propsPrefs = entityPrefs.node("properties");
for (PropertyDescriptor persistentProperty : state.entityDescriptor().state().properties()) {
if (persistentProperty.qualifiedName().name().equals("identity")) {
// Skip Identity.identity()
continue;
}
Object value = state.properties().get(persistentProperty.qualifiedName());
if (value == null) {
propsPrefs.remove(persistentProperty.qualifiedName().name());
} else {
ValueType valueType = persistentProperty.valueType();
Class<?> mainType = valueType.mainType();
if (Number.class.isAssignableFrom(mainType)) {
if (mainType.equals(Long.class)) {
propsPrefs.putLong(persistentProperty.qualifiedName().name(), (Long) value);
} else if (mainType.equals(Integer.class)) {
propsPrefs.putInt(persistentProperty.qualifiedName().name(), (Integer) value);
} else if (mainType.equals(Double.class)) {
propsPrefs.putDouble(persistentProperty.qualifiedName().name(), (Double) value);
} else if (mainType.equals(Float.class)) {
propsPrefs.putFloat(persistentProperty.qualifiedName().name(), (Float) value);
} else {
// Store as string even though it's a number
String jsonString = valueSerialization.serialize(value);
propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
}
} else if (mainType.equals(Boolean.class)) {
propsPrefs.putBoolean(persistentProperty.qualifiedName().name(), (Boolean) value);
} else if (valueType instanceof ValueCompositeType || valueType instanceof MapType || valueType instanceof CollectionType || valueType instanceof EnumType) {
String jsonString = valueSerialization.serialize(value);
propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
} else {
String jsonString = valueSerialization.serialize(value);
propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
}
}
}
// Associations
if (!state.associations().isEmpty()) {
Preferences assocsPrefs = entityPrefs.node("associations");
for (Map.Entry<QualifiedName, EntityReference> association : state.associations().entrySet()) {
if (association.getValue() == null) {
assocsPrefs.remove(association.getKey().name());
} else {
assocsPrefs.put(association.getKey().name(), association.getValue().identity());
}
}
}
// ManyAssociations
if (!state.manyAssociations().isEmpty()) {
Preferences manyAssocsPrefs = entityPrefs.node("manyassociations");
for (Map.Entry<QualifiedName, List<EntityReference>> manyAssociation : state.manyAssociations().entrySet()) {
StringBuilder manyAssocs = new StringBuilder();
for (EntityReference entityReference : manyAssociation.getValue()) {
if (manyAssocs.length() > 0) {
manyAssocs.append("\n");
}
manyAssocs.append(entityReference.identity());
}
if (manyAssocs.length() > 0) {
manyAssocsPrefs.put(manyAssociation.getKey().name(), manyAssocs.toString());
}
}
}
// NamedAssociations
if (!state.namedAssociations().isEmpty()) {
Preferences namedAssocsPrefs = entityPrefs.node("namedassociations");
for (Map.Entry<QualifiedName, Map<String, EntityReference>> namedAssociation : state.namedAssociations().entrySet()) {
StringBuilder namedAssocs = new StringBuilder();
for (Map.Entry<String, EntityReference> namedRef : namedAssociation.getValue().entrySet()) {
if (namedAssocs.length() > 0) {
namedAssocs.append("\n");
}
namedAssocs.append(namedRef.getKey()).append("\n").append(namedRef.getValue().identity());
}
if (namedAssocs.length() > 0) {
namedAssocsPrefs.put(namedAssociation.getKey().name(), namedAssocs.toString());
}
}
}
} catch (ValueSerializationException e) {
throw new EntityStoreException("Could not store EntityState", e);
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method putArrayNodeInMap.
@Override
protected <K, V> void putArrayNodeInMap(JsonNode inputNode, Function<JsonNode, K> keyDeserializer, Function<JsonNode, V> valueDeserializer, Map<K, V> map) throws Exception {
if (isNullOrMissing(inputNode)) {
return;
}
if (!inputNode.isArray()) {
throw new ValueSerializationException("Expected an array but got " + inputNode);
}
ArrayNode array = (ArrayNode) inputNode;
for (JsonNode item : array) {
if (!item.isObject()) {
throw new ValueSerializationException("Expected an object but got " + inputNode);
}
JsonNode keyNode = item.get("key");
JsonNode valueNode = item.get("value");
K key = keyDeserializer.map(keyNode);
V value = valueDeserializer.map(valueNode);
if (key != null) {
map.put(key, value);
}
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method putArrayNodeInCollection.
@Override
protected <T> void putArrayNodeInCollection(JsonNode inputNode, Function<JsonNode, T> deserializer, Collection<T> collection) throws Exception {
if (isNullOrMissing(inputNode)) {
return;
}
if (!inputNode.isArray()) {
throw new ValueSerializationException("Expected an array but got " + inputNode);
}
ArrayNode array = (ArrayNode) inputNode;
for (JsonNode item : array) {
T value = deserializer.map(item);
collection.add(value);
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method readMapInMap.
@Override
protected <K, V> Map<K, V> readMapInMap(JsonParser input, Function<JsonParser, K> keyDeserializer, Function<JsonParser, V> valueDeserializer, Map<K, V> map) throws Exception {
JsonToken token = input.getCurrentToken();
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
token = input.nextToken();
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
}
JsonToken currentToken = input.nextToken();
while (currentToken != JsonToken.END_ARRAY) {
if (currentToken != JsonToken.START_OBJECT) {
throw new ValueSerializationException("Expected an object start at " + input.getCurrentLocation().toString());
}
currentToken = input.nextToken();
K key = null;
V value = null;
while (currentToken != JsonToken.END_OBJECT) {
String objectKey = input.getCurrentName();
input.nextToken();
if ("key".equals(objectKey)) {
key = keyDeserializer.map(input);
} else if ("value".equals(objectKey)) {
value = valueDeserializer.map(input);
} else {
//input.nextToken();
input.skipChildren();
}
currentToken = input.nextToken();
}
if (key != null) {
map.put(key, value);
}
currentToken = input.nextToken();
}
return map;
}
Aggregations