diff --git a/src/com/esotericsoftware/yamlbeans/YamlReader.java b/src/com/esotericsoftware/yamlbeans/YamlReader.java index 6c510d8..351d4a6 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlReader.java +++ b/src/com/esotericsoftware/yamlbeans/YamlReader.java @@ -227,6 +227,55 @@ protected Class findTagClass (String tag, ClassLoader classLoader) throws Cla return Class.forName(tag, true, classLoader); } + /** + * Reads a scalar value from the parser and converts it to the specified type. + * @param type The type to convert the scalar to. + * @param anchor The anchor of the scalar, or null. + * @return The converted scalar value. + * @throws YamlException + * @throws ParserException + */ + private Object readScalarValue(Class type, String anchor) throws YamlException, ParserException { + 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 (value == null) { + convertedValue = null; + } else if (type == String.class) { + convertedValue = value; + } else if (type == Integer.TYPE || type == Integer.class) { + convertedValue = Integer.decode(value); + } else if (type == Boolean.TYPE || type == Boolean.class) { + convertedValue = Boolean.valueOf(value); + } else if (type == Float.TYPE || type == Float.class) { + convertedValue = Float.valueOf(value); + } else if (type == Double.TYPE || type == Double.class) { + convertedValue = Double.valueOf(value); + } else if (type == Long.TYPE || type == Long.class) { + convertedValue = Long.decode(value); + } else if (type == Short.TYPE || type == Short.class) { + convertedValue = Short.decode(value); + } else if (type == Character.TYPE || type == Character.class) { + convertedValue = value.charAt(0); + } else if (type == Byte.TYPE || type == Byte.class) { + convertedValue = Byte.decode(value); + } else { + throw new YamlException("Unknown field type."); + } + if (anchor != null) { + addAnchor(anchor, convertedValue); + } + return convertedValue; + } catch (Exception ex) { + throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex); + } + } + private Object readValueInternal (Class type, Class elementType, String anchor) throws YamlException, ParserException, TokenizerException { if (type == null || type == Object.class) { @@ -239,7 +288,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor) if (config.readConfig.guessNumberTypes) { String value = ((ScalarEvent)event).value; if (value != null) { - Number number = valueConvertedNumber(value); + Number number = parseNumber(value); if (number != null) { if (anchor != null) addAnchor(anchor, number); parser.getNextEvent(); @@ -258,39 +307,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor) } 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 (value == null) { - convertedValue = null; - } else if (type == String.class) { - convertedValue = value; - } else if (type == Integer.TYPE || type == Integer.class) { - convertedValue = Integer.decode(value); - } else if (type == Boolean.TYPE || type == Boolean.class) { - convertedValue = Boolean.valueOf(value); - } else if (type == Float.TYPE || type == Float.class) { - convertedValue = Float.valueOf(value); - } else if (type == Double.TYPE || type == Double.class) { - convertedValue = Double.valueOf(value); - } else if (type == Long.TYPE || type == Long.class) { - convertedValue = Long.decode(value); - } else if (type == Short.TYPE || type == Short.class) { - convertedValue = Short.decode(value); - } else if (type == Character.TYPE || type == Character.class) { - convertedValue = value.charAt(0); - } else if (type == Byte.TYPE || type == Byte.class) { - convertedValue = Byte.decode(value); - } else - throw new YamlException("Unknown field type."); - if (anchor != null) addAnchor(anchor, convertedValue); - return convertedValue; - } catch (Exception ex) { - throw new YamlReaderException("Unable to convert value to required type \"" + type + "\": " + value, ex); - } + return readScalarValue(type, anchor); } for (Entry entry : config.scalarSerializers.entrySet()) { @@ -486,7 +503,7 @@ public YamlReaderException (String message) { } } - private Number valueConvertedNumber (String value) { + private Number parseNumber(String value) { Number number = null; try { number = Long.decode(value); diff --git a/src/com/esotericsoftware/yamlbeans/parser/Parser.java b/src/com/esotericsoftware/yamlbeans/parser/Parser.java index 4845aa1..0ffedf2 100644 --- a/src/com/esotericsoftware/yamlbeans/parser/Parser.java +++ b/src/com/esotericsoftware/yamlbeans/parser/Parser.java @@ -127,15 +127,24 @@ public Event produce () { }; table[P_IMPLICIT_DOCUMENT] = new Production() { public Event produce () { - TokenType type = tokenizer.peekNextTokenType(); - if (!(type == DIRECTIVE || type == DOCUMENT_START || type == STREAM_END)) { - parseStack.add(0, table[P_DOCUMENT_END]); - parseStack.add(0, table[P_BLOCK_NODE]); - parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]); + if (isNextTokenImplicitDocument()) { + parseImplicitDocument(); } return null; } + + private boolean isNextTokenImplicitDocument() { + TokenType type = tokenizer.peekNextTokenType(); + return type != DIRECTIVE && type != DOCUMENT_START && type != STREAM_END; + } + + private void parseImplicitDocument() { + parseStack.add(0, table[P_DOCUMENT_END]); + parseStack.add(0, table[P_BLOCK_NODE]); + parseStack.add(0, table[P_DOCUMENT_START_IMPLICIT]); + } }; + table[P_EXPLICIT_DOCUMENT] = new Production() { public Event produce () { if (tokenizer.peekNextTokenType() != STREAM_END) {