Java Code Examples for com.fasterxml.jackson.core.io.NumberInput#parseLong()

The following examples show how to use com.fasterxml.jackson.core.io.NumberInput#parseLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
protected final long _parseLongPrimitive(DeserializationContext ctxt, String text) throws IOException
{
    try {
        return NumberInput.parseLong(text);
    } catch (IllegalArgumentException iae) { }
    {
        Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
                "not a valid long value");
        return _nonNullNumber(v).longValue();
    }
}
 
Example 2
Source File: StdDateFormat.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Date _parseDateFromLong(String longStr, ParsePosition pos) throws ParseException
{
    long ts;
    try {
        ts = NumberInput.parseLong(longStr);
    } catch (NumberFormatException e) {
        throw new ParseException(String.format(
                "Timestamp value %s out of 64-bit value range", longStr),
                pos.getErrorIndex());
    }
    return new Date(ts);
}
 
Example 3
Source File: JsonPointer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private final static int _parseIndex(String str) {
    final int len = str.length();
    // [core#133]: beware of super long indexes; assume we never
    // have arrays over 2 billion entries so ints are fine.
    if (len == 0 || len > 10) {
        return -1;
    }
    // [core#176]: no leading zeroes allowed
    char c = str.charAt(0);
    if (c <= '0') {
        return (len == 1 && c == '0') ? 0 : -1;
    }
    if (c > '9') {
        return -1;
    }
    for (int i = 1; i < len; ++i) {
        c = str.charAt(i);
        if (c > '9' || c < '0') {
            return -1;
        }
    }
    if (len == 10) {
        long l = NumberInput.parseLong(str);
        if (l > Integer.MAX_VALUE) {
            return -1;
        }
    }
    return NumberInput.parseInt(str);
}
 
Example 4
Source File: TextBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Specialized convenience method that will decode a 64-bit int,
 * of at most 18 digits (and possible leading minus sign).
 *
 * @param neg Whether contents start with a minus sign
 *
 * @since 2.9
 */
public long contentsAsLong(boolean neg) {
    if ((_inputStart >= 0) && (_inputBuffer != null)) {
        if (neg) {
            return -NumberInput.parseLong(_inputBuffer, _inputStart+1, _inputLen-1);
        }
        return NumberInput.parseLong(_inputBuffer, _inputStart, _inputLen);
    }
    if (neg) {
        return -NumberInput.parseLong(_currentSegment, 1, _currentSize-1);
    }
    return NumberInput.parseLong(_currentSegment, 0, _currentSize);
}
 
Example 5
Source File: ParseSupport.java    From curiostack with MIT License 5 votes vote down vote up
/** Parses a long out of the input, using the optimized path when the value is not quoted. */
private static long parseLong(JsonParser parser) throws IOException {
  if (parser.currentToken() == JsonToken.VALUE_NUMBER_INT) {
    return parser.getLongValue();
  }
  return NumberInput.parseLong(parser.getText());
}
 
Example 6
Source File: OptimizedSettableBeanProperty.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
protected final long _deserializeLong(JsonParser p, DeserializationContext ctxt)
    throws IOException
{
    switch (p.currentTokenId()) {
    case JsonTokenId.ID_NUMBER_INT:
        return p.getLongValue();
    case JsonTokenId.ID_NUMBER_FLOAT:
        if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
            _failDoubleToIntCoercion(p, ctxt, "long");
        }
        return p.getValueAsLong();
    case JsonTokenId.ID_STRING:
        _verifyScalarCoercion(ctxt, p, "long");
        String text = p.getText().trim();
        if (text.length() == 0 || _hasTextualNull(text)) {
            return 0L;
        }
        try {
            return NumberInput.parseLong(text);
        } catch (IllegalArgumentException iae) { }
        throw ctxt.weirdStringException(text, Long.TYPE, "not a valid long value");
    case JsonTokenId.ID_NULL:
        if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
            _failNullToPrimitiveCoercion(ctxt, "long");
        }
        return 0L;
    case JsonTokenId.ID_START_ARRAY:
        if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
            p.nextToken();
            final long parsed = _deserializeLong(p, ctxt);
            JsonToken t = p.nextToken();
            if (t != JsonToken.END_ARRAY) {
                _handleMissingEndArrayForSingle(p, ctxt);
            }            
            return parsed;
        }
        break;
    }
    return (Long) ctxt.handleUnexpectedToken(getType(), p);
}
 
Example 7
Source File: JsonPointer.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private final static int _parseIndex(String str) {
    final int len = str.length();
    // [core#133]: beware of super long indexes; assume we never
    // have arrays over 2 billion entries so ints are fine.
    if (len == 0 || len > 10) {
        return -1;
    }
    // [core#176]: no leading zeroes allowed
    char c = str.charAt(0);
    if (c <= '0') {
        return (len == 1 && c == '0') ? 0 : -1;
    }
    if (c > '9') {
        return -1;
    }
    for (int i = 1; i < len; ++i) {
        c = str.charAt(i);
        if (c > '9' || c < '0') {
            return -1;
        }
    }
    if (len == 10) {
        long l = NumberInput.parseLong(str);
        if (l > Integer.MAX_VALUE) {
            return -1;
        }
    }
    return NumberInput.parseInt(str);
}
 
Example 8
Source File: ParserBase.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Method that will parse actual numeric value out of a syntactically
 * valid number value. Type it will parse into depends on whether
 * it is a floating point number, as well as its magnitude: smallest
 * legal type (of ones available) is used for efficiency.
 *
 * @param expType Numeric type that we will immediately need, if any;
 *   mostly necessary to optimize handling of floating point numbers
 */
protected void _parseNumericValue(int expType) throws IOException
{
    // Int or float?
    if (_currToken == JsonToken.VALUE_NUMBER_INT) {
        char[] buf = _textBuffer.getTextBuffer();
        int offset = _textBuffer.getTextOffset();
        int len = _intLength;
        if (_numberNegative) {
            ++offset;
        }
        if (len <= 9) { // definitely fits in int
            int i = NumberInput.parseInt(buf, offset, len);
            _numberInt = _numberNegative ? -i : i;
            _numTypesValid = NR_INT;
            return;
        }
        if (len <= 18) { // definitely fits AND is easy to parse using 2 int parse calls
            long l = NumberInput.parseLong(buf, offset, len);
            if (_numberNegative) {
                l = -l;
            }
            // [JACKSON-230] Could still fit in int, need to check
            if (len == 10) {
                if (_numberNegative) {
                    if (l >= MIN_INT_L) {
                        _numberInt = (int) l;
                        _numTypesValid = NR_INT;
                        return;
                    }
                } else {
                    if (l <= MAX_INT_L) {
                        _numberInt = (int) l;
                        _numTypesValid = NR_INT;
                        return;
                    }
                }
            }
            _numberLong = l;
            _numTypesValid = NR_LONG;
            return;
        }
        _parseSlowInt(expType, buf, offset, len);
        return;
    }
    if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
        _parseSlowFloat(expType);
        return;
    }
    _reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors");
}