@@ -51,22 +51,22 @@ private DefaultTypeConverterFactory() {
5151 converters .put (String .class , Function .identity ()::apply );
5252 converters .put (boolean .class , DefaultTypeConverterFactory ::convertBoolean );
5353 converters .put (Boolean .class , DefaultTypeConverterFactory ::convertBoolean );
54- converters .put (Integer .class , Integer :: valueOf );
55- converters .put (int .class , Integer :: valueOf );
56- converters .put (long .class , Long :: valueOf );
57- converters .put (Long .class , Long :: valueOf );
58- converters .put (short .class , Short :: valueOf );
59- converters .put (Short .class , Short :: valueOf );
60- converters .put (byte .class , Byte :: valueOf );
61- converters .put (Byte .class , Byte :: valueOf );
62- converters .put (double .class , Double :: valueOf );
63- converters .put (Double .class , Double :: valueOf );
64- converters .put (float .class , Float :: valueOf );
65- converters .put (Float .class , Float :: valueOf );
54+ converters .put (Integer .class , Lenient :: parseInt );
55+ converters .put (int .class , Lenient :: parseInt );
56+ converters .put (long .class , Lenient :: parseLong );
57+ converters .put (Long .class , Lenient :: parseLong );
58+ converters .put (short .class , Lenient :: parseShort );
59+ converters .put (Short .class , Lenient :: parseShort );
60+ converters .put (byte .class , Lenient :: parseByte );
61+ converters .put (Byte .class , Lenient :: parseByte );
62+ converters .put (double .class , Lenient :: parseDouble );
63+ converters .put (Double .class , Lenient :: parseDouble );
64+ converters .put (float .class , Lenient :: parseFloat );
65+ converters .put (Float .class , Lenient :: parseFloat );
6666 converters .put (BigInteger .class , BigInteger ::new );
6767 converters .put (BigDecimal .class , BigDecimal ::new );
68- converters .put (AtomicInteger .class , v -> new AtomicInteger (Integer .parseInt (v )));
69- converters .put (AtomicLong .class , v -> new AtomicLong (Long .parseLong (v )));
68+ converters .put (AtomicInteger .class , v -> new AtomicInteger (Lenient .parseInt (v )));
69+ converters .put (AtomicLong .class , v -> new AtomicLong (Lenient .parseLong (v )));
7070 converters .put (Duration .class , Duration ::parse );
7171 converters .put (Period .class , Period ::parse );
7272 converters .put (LocalDateTime .class , LocalDateTime ::parse );
@@ -76,7 +76,7 @@ private DefaultTypeConverterFactory() {
7676 converters .put (OffsetTime .class , OffsetTime ::parse );
7777 converters .put (ZonedDateTime .class , ZonedDateTime ::parse );
7878 converters .put (Instant .class , v -> Instant .from (OffsetDateTime .parse (v )));
79- converters .put (Date .class , v -> new Date (Long .parseLong (v )));
79+ converters .put (Date .class , v -> new Date (Lenient .parseLong (v )));
8080 converters .put (Currency .class , Currency ::getInstance );
8181 converters .put (URI .class , URI ::create );
8282 converters .put (Locale .class , Locale ::forLanguageTag );
@@ -103,4 +103,45 @@ public Optional<TypeConverter<?>> get(Type type, TypeConverter.Registry registry
103103 }
104104 return Optional .empty ();
105105 }
106+
107+ /** A collection of lenient number parsers that allow whitespace and trailing 'L' or 'l' in long values */
108+ private static final class Lenient {
109+ private static String maybeTrim (String s ) {
110+ // The way these are called, we'll never get a null. In any case, we pass it through, to ensure that
111+ // the exception thrown remains the same as whatever the JDK's parse***() methods throw.
112+ return s != null ? s .trim () : null ;
113+ }
114+
115+ private static long parseLong (String s ) throws NumberFormatException {
116+ s = maybeTrim (s );
117+ // Also allow trailing 'L' or 'l' in long values
118+ if (s != null ) {
119+ if (s .endsWith ("L" ) || s .endsWith ("l" )) {
120+ s = s .substring (0 , s .length () - 1 );
121+ }
122+ }
123+
124+ return Long .parseLong (s );
125+ }
126+
127+ private static int parseInt (String s ) throws NumberFormatException {
128+ return Integer .parseInt (maybeTrim (s ));
129+ }
130+
131+ private static short parseShort (String s ) throws NumberFormatException {
132+ return Short .parseShort (maybeTrim (s ));
133+ }
134+
135+ private static byte parseByte (String s ) throws NumberFormatException {
136+ return Byte .parseByte (maybeTrim (s ));
137+ }
138+
139+ private static double parseDouble (String s ) throws NumberFormatException {
140+ return Double .parseDouble (maybeTrim (s ));
141+ }
142+
143+ private static float parseFloat (String s ) throws NumberFormatException {
144+ return Float .parseFloat (maybeTrim (s ));
145+ }
146+ }
106147}
0 commit comments