}
private Object readValueInternal (Class type, Class elementType, String anchor) throws YamlException, ParserException,
TokenizerException {
if (type == null || type == Object.class) {
Event event = parser.peekNextEvent();
switch (event.type) {
case MAPPING_START:
type = HashMap.class;
break;
case SCALAR:
type = String.class;
break;
case SEQUENCE_START:
type = ArrayList.class;
break;
default:
throw new YamlReaderException("Expected scalar, sequence, or mapping but found: " + event.type);
}
}
if (type == String.class) {
Event event = parser.getNextEvent();
if (event.type != SCALAR) throw new YamlReaderException("Expected scalar for String type but found: " + event.type);
String value = ((ScalarEvent)event).value;
if (anchor != null) anchors.put(anchor, value);
return value;
}
if (Beans.isScalar(type)) {
Event event = parser.getNextEvent();
if (event.type != SCALAR)
throw new YamlReaderException("Expected scalar for primitive type '" + type.getClass() + "' but found: " + event.type);
String value = ((ScalarEvent)event).value;
try {
Object convertedValue;
if (type == String.class) {
convertedValue = value;
} else if (type == Integer.TYPE) {
convertedValue = value.length() == 0 ? 0 : Integer.decode(value);
} else if (type == Integer.class) {
convertedValue = value.length() == 0 ? null : Integer.decode(value);
} else if (type == Boolean.TYPE) {
convertedValue = value.length() == 0 ? false : Boolean.valueOf(value);
} else if (type == Boolean.class) {
convertedValue = value.length() == 0 ? null : Boolean.valueOf(value);
} else if (type == Float.TYPE) {
convertedValue = value.length() == 0 ? 0 : Float.valueOf(value);
} else if (type == Float.class) {
convertedValue = value.length() == 0 ? null : Float.valueOf(value);
} else if (type == Double.TYPE) {
convertedValue = value.length() == 0 ? 0 : Double.valueOf(value);
} else if (type == Double.class) {
convertedValue = value.length() == 0 ? null : Double.valueOf(value);
} else if (type == Long.TYPE) {
convertedValue = value.length() == 0 ? 0 : Long.decode(value);
} else if (type == Long.class) {
convertedValue = value.length() == 0 ? null : Long.decode(value);
} else if (type == Short.TYPE) {
convertedValue = value.length() == 0 ? 0 : Short.decode(value);
} else if (type == Short.class) {
convertedValue = value.length() == 0 ? null : Short.decode(value);
} else if (type == Character.TYPE) {
convertedValue = value.length() == 0 ? 0 : value.charAt(0);
} else if (type == Character.class) {
convertedValue = value.length() == 0 ? null : value.charAt(0);
} else if (type == Byte.TYPE) {
convertedValue = value.length() == 0 ? 0 : Byte.decode(value);
} else if (type == Byte.class) {
convertedValue = value.length() == 0 ? null : Byte.decode(value);
} else
throw new YamlException("Unknown field type.");
if (anchor != null) anchors.put(anchor, convertedValue);
return convertedValue;
} catch (Exception ex) {
throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex);
}
}
if (Enum.class.isAssignableFrom(type)) {
Event event = parser.getNextEvent();
if (event.type != SCALAR) throw new YamlReaderException("Expected scalar for enum type but found: " + event.type);
String enumValueName = ((ScalarEvent)event).value;
if (enumValueName.length() == 0) return null;
try {
return Enum.valueOf(type, enumValueName);
} catch (Exception ex) {
throw new YamlReaderException("Unable to find enum value '" + enumValueName + "' for enum class: " + type.getName());
}
}
for (Entry<Class, ScalarSerializer> entry : config.scalarSerializers.entrySet()) {
if (entry.getKey().isAssignableFrom(type)) {
ScalarSerializer serializer = entry.getValue();
Event event = parser.getNextEvent();
if (event.type != SCALAR)
throw new YamlReaderException("Expected scalar for type '" + type + "' to be deserialized by scalar serializer '"
+ serializer.getClass().getName() + "' but found: " + event.type);
Object value = serializer.read(((ScalarEvent)event).value);
if (anchor != null) anchors.put(anchor, value);
return value;
}
}
Event event = parser.peekNextEvent();
switch (event.type) {
case MAPPING_START: {
// Must be a map or an object.
event = parser.getNextEvent();
Object object;