private Number readNumber ( int c ) throws IOException { final FastPushbackReader in = input ; final StringBuilder number = numBuf ; number . setLength ( 0 ) ; number . appendCodePoint ( c ) ; boolean isFloat = false ; if ( JsonReader . isLenient ( ) && ( c == ' ' || c == ' ' || c == ' ' ) ) { // Handle negativity. final boolean isNeg = ( c == ' ' ) ; if ( isNeg ) { // Advance to next character. c = input . read ( ) ; } // Case "-Infinity", "Infinity" or "NaN". if ( c == ' ' ) { // [Out of RFC 4627] accept NaN/Infinity values readToken ( "infinity" ) ; return ( isNeg ) ? Double . NEGATIVE_INFINITY : Double . POSITIVE_INFINITY ; } else if ( ' ' == c ) { // [Out of RFC 4627] accept NaN/Infinity values readToken ( "nan" ) ; return Double . NaN ; } else { // Case this is a number but not "-Infinity", like "-2". We let the normal code process. input . unread ( ' ' ) ; } } while ( true ) { c = in . read ( ) ; if ( ( c >= ' ' && c <= ' ' ) || c == ' ' || c == ' ' ) { number . appendCodePoint ( c ) ; } else if ( c == ' ' || c == ' ' || c == ' ' ) { number . appendCodePoint ( c ) ; isFloat = true ; } else if ( c == - 1 ) { break ; } else { in . unread ( c ) ; break ; } } try { if ( isFloat ) { // Floating point number needed return Double . parseDouble ( number . toString ( ) ) ; } else { return Long . parseLong ( number . toString ( ) ) ; } } catch ( Exception e ) { return ( Number ) error ( "Invalid number: " + number , e ) ; } }
Read a JSON number