@Override
  public Number nextNumber()
  {
    // TODO needs better handling for BigInteger and BigDecimal.
    // Use of DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS and USE_BIG_INTEGER_FOR_INTS. See NumberDeserializer of Jackson.

    int p = peeked;
    if (p == PEEKED_NONE) {
      p = doPeek();
    }

    Number result;
    if (p == PEEKED_LONG) {
      if (peekedLong < 0l) {
        if (peekedLong >= MIN_INT_L) {
           result = (int) peekedLong;
        } else {
            result = peekedLong;
        }
      } else {
        if (peekedLong <= MAX_INT_L) {
          result = (int) peekedLong;
        } else {
          result = peekedLong;
        }
      }
      peeked = PEEKED_NONE;
      return result;
    }

    if (p == PEEKED_NUMBER) {
      peekedString = new String(buffer, pos, peekedNumberLength);
      pos += peekedNumberLength;
      peeked = PEEKED_BUFFERED;
      result = Double.parseDouble( peekedString );
      peekedString = null;
      peeked = PEEKED_NONE;
      return result;
    }

    if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
      peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
    } else if (p == PEEKED_UNQUOTED) {
      peekedString = nextUnquotedValue();
    } else if (p != PEEKED_BUFFERED) {
      throw new IllegalStateException("Expected a double but was " + peek()
          + " at line " + getLineNumber() + " column " + getColumnNumber());
    }

    peeked = PEEKED_BUFFERED;
    if (peekedString.contains( "." )) {
      // decimal
      double resultDouble = Double.parseDouble( peekedString ); // don't catch this NumberFormatException.
      if (!lenient && (Double.isNaN( resultDouble ) || Double.isInfinite( resultDouble ))) {
        throw syntaxError( "JSON forbids NaN and infinities: " + resultDouble);
      }
      result = resultDouble;
    } else {
      int length = peekedString.length();
      if (length <= 9) { // fits in int
        result = Integer.parseInt( peekedString );
      } else if (length <= 18) { // fits in long and potentially int
        long longResult = Long.parseLong( peekedString );
        if(length == 10) { // can fits in int
          if (longResult < 0l) {
            if (longResult >= MIN_INT_L) {
             result = (int) longResult;
            } else {
             result = longResult;
            }
          } else {
            if (longResult <= MAX_INT_L) {
              result = (int) longResult;
            } else {
              result = longResult;
            }
          }
        } else {
          result = longResult;
        }
      } else {
        BigInteger bigIntegerResult = new BigInteger( peekedString );
        if (bigIntegerResult.signum() == -1) {
          if (bigIntegerResult.compareTo( MIN_LONG_BIGINTEGER ) >= 0) {
           result = bigIntegerResult.longValue();
          } else {
           result = bigIntegerResult;
          }
        } else {
          if (bigIntegerResult.compareTo( MAX_LONG_BIGINTEGER) <= 0) {
            result = bigIntegerResult.longValue();
          } else {
            result = bigIntegerResult;
          }
        }
      }
    }
    peekedString = null;
    peeked = PEEKED_NONE;
    return result;
  }
@ Override public Number nextNumber ( ) { int p = peeked ; if ( p == PEEKED_NONE ) { p = doPeek ( ) ; } Number result ; if ( p == PEEKED_LONG ) { if ( peekedLong < 0l ) { if ( peekedLong >= MIN_INT_L ) { result = ( int ) peekedLong ; } else { result = peekedLong ; } } else { if ( peekedLong <= MAX_INT_L ) { result = ( int ) peekedLong ; } else { result = peekedLong ; } } peeked = PEEKED_NONE ; return result ; } if ( p == PEEKED_NUMBER ) { peekedString = new String ( buffer , pos , peekedNumberLength ) ; pos += peekedNumberLength ; peeked = PEEKED_BUFFERED ; result = Double . parseDouble ( peekedString ) ; peekedString = null ; peeked = PEEKED_NONE ; return result ; } if ( p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED ) { peekedString = nextQuotedValue ( p == PEEKED_SINGLE_QUOTED ? '\'' : '"' ) ; } else if ( p == PEEKED_UNQUOTED ) { peekedString = nextUnquotedValue ( ) ; } else if ( p != PEEKED_BUFFERED ) { throw new IllegalStateException ( "Expected a double but was " + peek ( ) + " at line " + getLineNumber ( ) + " column " + getColumnNumber ( ) ) ; } peeked = PEEKED_BUFFERED ; if ( peekedString . contains ( "." ) ) { double resultDouble = Double . parseDouble ( peekedString ) ; if ( ! lenient && ( Double . isNaN ( resultDouble ) || Double . isInfinite ( resultDouble ) ) ) { throw syntaxError ( "JSON forbids NaN and infinities: " + resultDouble ) ; } result = resultDouble ; } else { int length = peekedString . length ( ) ; if ( length <= 9 ) { result = Integer . parseInt ( peekedString ) ; } else if ( length <= 18 ) { long longResult = Long . parseLong ( peekedString ) ; if ( length == 10 ) { if ( longResult < 0l ) { if ( longResult >= MIN_INT_L ) { result = ( int ) longResult ; } else { result = longResult ; } } else { if ( longResult <= MAX_INT_L ) { result = ( int ) longResult ; } else { result = longResult ; } } } else { result = longResult ; } } else { BigInteger bigIntegerResult = new BigInteger ( peekedString ) ; if ( bigIntegerResult . signum ( ) == - 1 ) { if ( bigIntegerResult . compareTo ( MIN_LONG_BIGINTEGER ) >= 0 ) { result = bigIntegerResult . longValue ( ) ; } else { result = bigIntegerResult ; } } else { if ( bigIntegerResult . compareTo ( MAX_LONG_BIGINTEGER ) <= 0 ) { result = bigIntegerResult . longValue ( ) ; } else { result = bigIntegerResult ; } } } } peekedString = null ; peeked = PEEKED_NONE ; return result ; }
{@inheritDoc}