use of org.qi4j.api.type.ValueType 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);
}
use of org.qi4j.api.type.ValueType in project qi4j-sdk by Qi4j.
the class AbstractCollectionSerializationTest method givenMapOfStringByteAndNullElementWhenSerializingAndDeserializingExpectEquals.
@Test
public void givenMapOfStringByteAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
String output = valueSerialization.serialize(stringByteMap());
MapType mapType = new MapType(Map.class, new ValueType(String.class), new ValueType(Byte.class));
Map<String, Byte> value = valueSerialization.deserialize(mapType, output);
assertEquals(stringByteMap(), value);
}
use of org.qi4j.api.type.ValueType in project qi4j-sdk by Qi4j.
the class AbstractCollectionSerializationTest method givenCollectionTypeWithLongAndNullElementWhenSerializingAndDeserializingExpectEquals.
@Test
public void givenCollectionTypeWithLongAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
String output = valueSerialization.serialize(longCollection());
CollectionType collectionType = new CollectionType(List.class, new ValueType(Long.class));
List<Long> list = valueSerialization.deserialize(collectionType, output);
assertEquals(longCollection(), list);
}
use of org.qi4j.api.type.ValueType in project qi4j-sdk by Qi4j.
the class ValueTypeFactory method newValueType.
public ValueType newValueType(Type type, Class declaringClass, Class compositeType, LayerModel layer, ModuleModel module) {
ValueType valueType = null;
if (CollectionType.isCollection(type)) {
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type collectionType = pt.getActualTypeArguments()[0];
if (collectionType instanceof TypeVariable) {
TypeVariable collectionTypeVariable = (TypeVariable) collectionType;
collectionType = Classes.resolveTypeVariable(collectionTypeVariable, declaringClass, compositeType);
}
ValueType collectedType = newValueType(collectionType, declaringClass, compositeType, layer, module);
valueType = new CollectionType(Classes.RAW_CLASS.map(type), collectedType);
} else {
valueType = new CollectionType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module));
}
} else if (MapType.isMap(type)) {
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type keyType = pt.getActualTypeArguments()[0];
if (keyType instanceof TypeVariable) {
TypeVariable keyTypeVariable = (TypeVariable) keyType;
keyType = Classes.resolveTypeVariable(keyTypeVariable, declaringClass, compositeType);
}
ValueType keyedType = newValueType(keyType, declaringClass, compositeType, layer, module);
Type valType = pt.getActualTypeArguments()[1];
if (valType instanceof TypeVariable) {
TypeVariable valueTypeVariable = (TypeVariable) valType;
valType = Classes.resolveTypeVariable(valueTypeVariable, declaringClass, compositeType);
}
ValueType valuedType = newValueType(valType, declaringClass, compositeType, layer, module);
valueType = new MapType(Classes.RAW_CLASS.map(type), keyedType, valuedType);
} else {
valueType = new MapType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module), newValueType(Object.class, declaringClass, compositeType, layer, module));
}
} else if (ValueCompositeType.isValueComposite(type)) {
// Find ValueModel in module/layer/used layers
ValueModel model = new ValueFinder(layer, module, Classes.RAW_CLASS.map(type)).getFoundModel();
if (model == null) {
if (type.equals(ValueComposite.class)) {
// Create default model
MixinsModel mixinsModel = new MixinsModel();
Iterable valueComposite = (Iterable) Iterables.iterable(ValueComposite.class);
model = new ValueModel(valueComposite, Visibility.application, new MetaInfo(), mixinsModel, new ValueStateModel(new PropertiesModel(), new AssociationsModel(), new ManyAssociationsModel()), new CompositeMethodsModel(mixinsModel));
} else {
throw new InvalidApplicationException("[" + module.name() + "] Could not find ValueComposite of type " + type);
}
}
return model.valueType();
} else if (EnumType.isEnum(type)) {
valueType = new EnumType(Classes.RAW_CLASS.map(type));
} else {
valueType = new ValueType(Classes.RAW_CLASS.map(type));
}
return valueType;
}
use of org.qi4j.api.type.ValueType in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serializeValueComposite.
private void serializeValueComposite(Resource subject, URI predicate, ValueComposite value, ValueType valueType, Graph graph, String baseUri, boolean includeNonQueryable) {
final ValueFactory valueFactory = graph.getValueFactory();
BNode collection = valueFactory.createBNode();
graph.add(subject, predicate, collection);
for (PropertyDescriptor persistentProperty : ((ValueCompositeType) valueType).properties()) {
Object propertyValue = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((Composite) value).state().propertyFor(persistentProperty.accessor()).get();
if (propertyValue == null) {
// Skip null values
continue;
}
ValueType type = persistentProperty.valueType();
if (type instanceof ValueCompositeType) {
URI pred = valueFactory.createURI(baseUri, persistentProperty.qualifiedName().name());
serializeValueComposite(collection, pred, (ValueComposite) propertyValue, type, graph, baseUri + persistentProperty.qualifiedName().name() + "/", includeNonQueryable);
} else {
serializeProperty(persistentProperty, propertyValue, collection, graph, includeNonQueryable);
}
}
}
Aggregations