@@ -227,6 +227,55 @@ protected Class<?> findTagClass (String tag, ClassLoader classLoader) throws Cla
227227 return Class .forName (tag , true , classLoader );
228228 }
229229
230+ /**
231+ * Reads a scalar value from the parser and converts it to the specified type.
232+ * @param type The type to convert the scalar to.
233+ * @param anchor The anchor of the scalar, or null.
234+ * @return The converted scalar value.
235+ * @throws YamlException
236+ * @throws ParserException
237+ */
238+ private Object readScalarValue (Class <?> type , String anchor ) throws YamlException , ParserException {
239+ Event event = parser .getNextEvent ();
240+ if (event .type != SCALAR ) {
241+ throw new YamlReaderException ("Expected scalar for primitive type '" + type .getClass () + "' but found: " + event .type );
242+ }
243+
244+ String value = ((ScalarEvent ) event ).value ;
245+ try {
246+ Object convertedValue ;
247+ if (value == null ) {
248+ convertedValue = null ;
249+ } else if (type == String .class ) {
250+ convertedValue = value ;
251+ } else if (type == Integer .TYPE || type == Integer .class ) {
252+ convertedValue = Integer .decode (value );
253+ } else if (type == Boolean .TYPE || type == Boolean .class ) {
254+ convertedValue = Boolean .valueOf (value );
255+ } else if (type == Float .TYPE || type == Float .class ) {
256+ convertedValue = Float .valueOf (value );
257+ } else if (type == Double .TYPE || type == Double .class ) {
258+ convertedValue = Double .valueOf (value );
259+ } else if (type == Long .TYPE || type == Long .class ) {
260+ convertedValue = Long .decode (value );
261+ } else if (type == Short .TYPE || type == Short .class ) {
262+ convertedValue = Short .decode (value );
263+ } else if (type == Character .TYPE || type == Character .class ) {
264+ convertedValue = value .charAt (0 );
265+ } else if (type == Byte .TYPE || type == Byte .class ) {
266+ convertedValue = Byte .decode (value );
267+ } else {
268+ throw new YamlException ("Unknown field type." );
269+ }
270+ if (anchor != null ) {
271+ addAnchor (anchor , convertedValue );
272+ }
273+ return convertedValue ;
274+ } catch (Exception ex ) {
275+ throw new YamlReaderException ("Unable to convert value to required type \" " + type + "\" : " + value , ex );
276+ }
277+ }
278+
230279 private Object readValueInternal (Class type , Class elementType , String anchor )
231280 throws YamlException , ParserException , TokenizerException {
232281 if (type == null || type == Object .class ) {
@@ -239,7 +288,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
239288 if (config .readConfig .guessNumberTypes ) {
240289 String value = ((ScalarEvent )event ).value ;
241290 if (value != null ) {
242- Number number = valueConvertedNumber (value );
291+ Number number = parseNumber (value );
243292 if (number != null ) {
244293 if (anchor != null ) addAnchor (anchor , number );
245294 parser .getNextEvent ();
@@ -258,39 +307,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
258307 }
259308
260309 if (Beans .isScalar (type )) {
261- Event event = parser .getNextEvent ();
262- if (event .type != SCALAR ) throw new YamlReaderException (
263- "Expected scalar for primitive type '" + type .getClass () + "' but found: " + event .type );
264- String value = ((ScalarEvent )event ).value ;
265- try {
266- Object convertedValue ;
267- if (value == null ) {
268- convertedValue = null ;
269- } else if (type == String .class ) {
270- convertedValue = value ;
271- } else if (type == Integer .TYPE || type == Integer .class ) {
272- convertedValue = Integer .decode (value );
273- } else if (type == Boolean .TYPE || type == Boolean .class ) {
274- convertedValue = Boolean .valueOf (value );
275- } else if (type == Float .TYPE || type == Float .class ) {
276- convertedValue = Float .valueOf (value );
277- } else if (type == Double .TYPE || type == Double .class ) {
278- convertedValue = Double .valueOf (value );
279- } else if (type == Long .TYPE || type == Long .class ) {
280- convertedValue = Long .decode (value );
281- } else if (type == Short .TYPE || type == Short .class ) {
282- convertedValue = Short .decode (value );
283- } else if (type == Character .TYPE || type == Character .class ) {
284- convertedValue = value .charAt (0 );
285- } else if (type == Byte .TYPE || type == Byte .class ) {
286- convertedValue = Byte .decode (value );
287- } else
288- throw new YamlException ("Unknown field type." );
289- if (anchor != null ) addAnchor (anchor , convertedValue );
290- return convertedValue ;
291- } catch (Exception ex ) {
292- throw new YamlReaderException ("Unable to convert value to required type \" " + type + "\" : " + value , ex );
293- }
310+ return readScalarValue (type , anchor );
294311 }
295312
296313 for (Entry <Class , ScalarSerializer > entry : config .scalarSerializers .entrySet ()) {
@@ -486,7 +503,7 @@ public YamlReaderException (String message) {
486503 }
487504 }
488505
489- private Number valueConvertedNumber (String value ) {
506+ private Number parseNumber (String value ) {
490507 Number number = null ;
491508 try {
492509 number = Long .decode (value );
0 commit comments