コード例 #1
0
  @Override
  public Number getNumberValue() throws IOException, JsonParseException {
    if (_numTypesValid == NR_UNKNOWN) {
      _parseNumericValue(NR_UNKNOWN); // will also check event type
    }
    // Separate types for int types
    if (_currToken == JsonToken.VALUE_NUMBER_INT) {
      if ((_numTypesValid & NR_INT) != 0) {
        return Integer.valueOf(_numberInt);
      }
      if ((_numTypesValid & NR_LONG) != 0) {
        return Long.valueOf(_numberLong);
      }
      if ((_numTypesValid & NR_BIGINT) != 0) {
        return _numberBigInt;
      }
      // Shouldn't get this far but if we do
      return _numberBigDecimal;
    }

    /* And then floating point types. But here optimal type
     * needs to be big decimal, to avoid losing any data?
     */
    if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
      return _numberBigDecimal;
    }
    if ((_numTypesValid & NR_DOUBLE) == 0) { // sanity check
      _throwInternal();
    }
    return Double.valueOf(_numberDouble);
  }
コード例 #2
0
 private final void _parseSlowIntValue(int expType, char[] buf, int offset, int len)
     throws IOException, JsonParseException {
   String numStr = _textBuffer.contentsAsString();
   try {
     // [JACKSON-230] Some long cases still...
     if (NumberInput.inLongRange(buf, offset, len, _numberNegative)) {
       // Probably faster to construct a String, call parse, than to use BigInteger
       _numberLong = Long.parseLong(numStr);
       _numTypesValid = NR_LONG;
     } else {
       // nope, need the heavy guns... (rare case)
       _numberBigInt = new BigInteger(numStr);
       _numTypesValid = NR_BIGINT;
     }
   } catch (NumberFormatException nex) {
     // Can this ever occur? Due to overflow, maybe?
     _wrapError("Malformed numeric value '" + numStr + "'", nex);
   }
 }