@@ -2216,61 +2216,42 @@ protected static boolean isDecimalNotation(final String val) {
22162216 * caller should catch this and wrap it in a {@link JSONException} if applicable.
22172217 */
22182218 protected static Number stringToNumber (final String val ) throws NumberFormatException {
2219- char initial = val .charAt (0 );
2220- if ((initial >= '0' && initial <= '9' ) || initial == '-' ) {
2219+ String trimmedVal = val .trim ();
2220+ char initial = trimmedVal .charAt (0 );
2221+ if (trimmedVal .matches ("-?\\ d?(\\ .\\ d+)?" )) {
22212222 // decimal representation
2222- if (isDecimalNotation (val )) {
2223+ if (isDecimalNotation (trimmedVal )) {
22232224 // Use a BigDecimal all the time so we keep the original
22242225 // representation. BigDecimal doesn't support -0.0, ensure we
22252226 // keep that by forcing a decimal.
22262227 try {
2227- BigDecimal bd = new BigDecimal (val );
2228+ BigDecimal bd = new BigDecimal (trimmedVal );
22282229 if (initial == '-' && BigDecimal .ZERO .compareTo (bd )==0 ) {
2229- return Double . valueOf ( -0.0 ) ;
2230+ return -0.0 ;
22302231 }
22312232 return bd ;
22322233 } catch (NumberFormatException retryAsDouble ) {
2233- // this is to support "Hex Floats" like this: 0x1.0P-1074
2234- try {
2235- Double d = Double .valueOf (val );
2236- if (d .isNaN () || d .isInfinite ()) {
2237- throw new NumberFormatException ("val [" +val +"] is not a valid number." );
2238- }
2239- return d ;
2240- } catch (NumberFormatException ignore ) {
2241- throw new NumberFormatException ("val [" +val +"] is not a valid number." );
2242- }
2243- }
2244- }
2245- // block items like 00 01 etc. Java number parsers treat these as Octal.
2246- if (initial == '0' && val .length () > 1 ) {
2247- char at1 = val .charAt (1 );
2248- if (at1 >= '0' && at1 <= '9' ) {
22492234 throw new NumberFormatException ("val [" +val +"] is not a valid number." );
22502235 }
2251- } else if (initial == '-' && val .length () > 2 ) {
2252- char at1 = val .charAt (1 );
2253- char at2 = val .charAt (2 );
2254- if (at1 == '0' && at2 >= '0' && at2 <= '9' ) {
2255- throw new NumberFormatException ("val [" +val +"] is not a valid number." );
2236+ } else {
2237+ // integer representation.
2238+ // This will narrow any values to the smallest reasonable Object representation
2239+ // (Integer, Long, or BigInteger)
2240+
2241+ // BigInteger down conversion: We use a similar bitLength compare as
2242+ // BigInteger#intValueExact uses. Increases GC, but objects hold
2243+ // only what they need. i.e. Less runtime overhead if the value is
2244+ // long lived.
2245+
2246+ BigInteger bi = new BigInteger (trimmedVal );
2247+ if (bi .bitLength () <= 31 ){
2248+ return bi .intValue ();
22562249 }
2250+ if (bi .bitLength () <= 63 ){
2251+ return bi .longValue ();
2252+ }
2253+ return bi ;
22572254 }
2258- // integer representation.
2259- // This will narrow any values to the smallest reasonable Object representation
2260- // (Integer, Long, or BigInteger)
2261-
2262- // BigInteger down conversion: We use a similar bitLength compare as
2263- // BigInteger#intValueExact uses. Increases GC, but objects hold
2264- // only what they need. i.e. Less runtime overhead if the value is
2265- // long lived.
2266- BigInteger bi = new BigInteger (val );
2267- if (bi .bitLength () <= 31 ){
2268- return Integer .valueOf (bi .intValue ());
2269- }
2270- if (bi .bitLength () <= 63 ){
2271- return Long .valueOf (bi .longValue ());
2272- }
2273- return bi ;
22742255 }
22752256 throw new NumberFormatException ("val [" +val +"] is not a valid number." );
22762257 }
0 commit comments