ScalarNode keyNode;
if (tuple.getKeyNode() instanceof ScalarNode) {
// key must be scalar
keyNode = (ScalarNode) tuple.getKeyNode();
} else {
throw new YAMLException("Keys must be scalars but found: " + tuple.getKeyNode());
}
Node valueNode = tuple.getValueNode();
// keys can only be Strings
keyNode.setType(String.class);
String key = (String) constructObject(keyNode);
try {
Property property = getProperty(beanType, key);
valueNode.setType(property.getType());
TypeDescription memberDescription = typeDefinitions.get(beanType);
boolean typeDetected = false;
if (memberDescription != null) {
switch (valueNode.getNodeId()) {
case sequence:
SequenceNode snode = (SequenceNode) valueNode;
Class<? extends Object> memberType = memberDescription
.getListPropertyType(key);
if (memberType != null) {
snode.setListType(memberType);
typeDetected = true;
} else if (property.getType().isArray()) {
snode.setListType(property.getType().getComponentType());
typeDetected = true;
}
break;
case mapping:
MappingNode mnode = (MappingNode) valueNode;
Class<? extends Object> keyType = memberDescription.getMapKeyType(key);
if (keyType != null) {
mnode.setTypes(keyType, memberDescription.getMapValueType(key));
typeDetected = true;
}
break;
default: // scalar
}
}
if (!typeDetected && valueNode.getNodeId() != NodeId.scalar) {
// only if there is no explicit TypeDescription
Class<?>[] arguments = property.getActualTypeArguments();
if (arguments != null && arguments.length > 0) {
// type safe (generic) collection may contain the
// proper class
if (valueNode.getNodeId() == NodeId.sequence) {
Class<?> t = arguments[0];
SequenceNode snode = (SequenceNode) valueNode;
snode.setListType(t);
} else if (valueNode.getTag().equals(Tag.SET)) {
Class<?> t = arguments[0];
MappingNode mnode = (MappingNode) valueNode;
mnode.setOnlyKeyType(t);
mnode.setUseClassConstructor(true);
} else if (property.getType().isAssignableFrom(Map.class)) {
Class<?> ketType = arguments[0];
Class<?> valueType = arguments[1];
MappingNode mnode = (MappingNode) valueNode;
mnode.setTypes(ketType, valueType);
mnode.setUseClassConstructor(true);
} else {
// the type for collection entries cannot be
// detected
}
}
}
Object value = constructObject(valueNode);
property.set(object, value);
} catch (Exception e) {
throw new YAMLException("Cannot create property=" + key + " for JavaBean="
+ object + "; " + e.getMessage(), e);
}
}
return object;
}