com.fasterxml.jackson.core.io.NumberInput Java Examples

The following examples show how to use com.fasterxml.jackson.core.io.NumberInput. 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: ParserBase.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
private void _parseSlowInt(int expType, char[] buf, int offset, int len) throws IOException
{
    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);
    }
}
 
Example #2
Source File: TextBuffer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convenience method for converting contents of the buffer
 * into a {@link BigDecimal}.
 */
public BigDecimal contentsAsDecimal() throws NumberFormatException
{
    // Already got a pre-cut array?
    if (_resultArray != null) {
        return NumberInput.parseBigDecimal(_resultArray);
    }
    // Or a shared buffer?
    if ((_inputStart >= 0) && (_inputBuffer != null)) {
        return NumberInput.parseBigDecimal(_inputBuffer, _inputStart, _inputLen);
    }
    // Or if not, just a single buffer (the usual case)
    if ((_segmentSize == 0) && (_currentSegment != null)) {
        return NumberInput.parseBigDecimal(_currentSegment, 0, _currentSize);
    }
    // If not, let's just get it aggregated...
    return NumberInput.parseBigDecimal(contentsAsArray());
}
 
Example #3
Source File: ParserBase.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void _parseSlowInt(int expType) throws IOException
{
    String numStr = _textBuffer.contentsAsString();
    try {
        int len = _intLength;
        char[] buf = _textBuffer.getTextBuffer();
        int offset = _textBuffer.getTextOffset();
        if (_numberNegative) {
            ++offset;
        }
        // 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);
    }
}
 
Example #4
Source File: StdDateFormat.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected Date _parseDate(String dateStr, ParsePosition pos) throws ParseException
{
    if (looksLikeISO8601(dateStr)) { // also includes "plain"
        return parseAsISO8601(dateStr, pos);
    }
    // Also consider "stringified" simple time stamp
    int i = dateStr.length();
    while (--i >= 0) {
        char ch = dateStr.charAt(i);
        if (ch < '0' || ch > '9') {
            // 07-Aug-2013, tatu: And [databind#267] points out that negative numbers should also work
            if (i > 0 || ch != '-') {
                break;
            }
        }
    }
    if ((i < 0)
        // let's just assume negative numbers are fine (can't be RFC-1123 anyway); check length for positive
            && (dateStr.charAt(0) == '-' || NumberInput.inLongRange(dateStr, false))) {
        return _parseDateFromLong(dateStr, pos);
    }
    // Otherwise, fall back to using RFC 1123. NOTE: call will NOT throw, just returns `null`
    return parseAsRFC1123(dateStr, pos);
}
 
Example #5
Source File: TextBuffer.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convenience method for converting contents of the buffer
 * into a {@link BigDecimal}.
 */
public BigDecimal contentsAsDecimal() throws NumberFormatException
{
    // Already got a pre-cut array?
    if (_resultArray != null) {
        return NumberInput.parseBigDecimal(_resultArray);
    }
    // Or a shared buffer?
    if ((_inputStart >= 0) && (_inputBuffer != null)) {
        return NumberInput.parseBigDecimal(_inputBuffer, _inputStart, _inputLen);
    }
    // Or if not, just a single buffer (the usual case)
    if ((_segmentSize == 0) && (_currentSegment != null)) {
        return NumberInput.parseBigDecimal(_currentSegment, 0, _currentSize);
    }
    // If not, let's just get it aggregated...
    return NumberInput.parseBigDecimal(contentsAsArray());
}
 
Example #6
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 6 votes vote down vote up
private Long rangeCheckedLong(JsonParser parser, DeserializationContext context)
throws IOException {
    String text = parser.getText().trim();
    if (text.length() == 0) {
        return null;
    }
    try {
        return Long.valueOf(NumberInput.parseLong(text));
    }
    catch (Exception e) {
        throw context.weirdStringException(//NOSONAR
            _valueClass,
            "Over/underflow: numeric value (" + text +") out of range of Long ("
                + Long.MIN_VALUE + " to " + Long.MAX_VALUE + ")"
        );
    }
}
 
Example #7
Source File: ParserMinimalBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double getValueAsDouble(double defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0L;
            }
            return NumberInput.parseAsDouble(str, defaultValue);
        case ID_NUMBER_INT:
        case ID_NUMBER_FLOAT:
            return getDoubleValue();
        case ID_TRUE:
            return 1.0;
        case ID_FALSE:
        case ID_NULL:
            return 0.0;
        case ID_EMBEDDED_OBJECT:
            Object value = this.getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).doubleValue();
            }
        }
    }
    return defaultValue;
}
 
Example #8
Source File: ParserMinimalBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long getValueAsLong(long defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return getLongValue();
    }
    if (t == JsonToken.VALUE_NUMBER_FLOAT) {
        return getLongValue();
    }
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0L;
            }
            return NumberInput.parseAsLong(str, defaultValue);
        case ID_TRUE:
            return 1L;
        case ID_FALSE:
        case ID_NULL:
            return 0L;
        case ID_EMBEDDED_OBJECT:
            Object value = this.getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).longValue();
            }
        }
    }
    return defaultValue;
}
 
Example #9
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public Integer deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonToken t = parser.getCurrentToken();
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
        return rangeCheckedInteger(parser, context);
    }
    if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
        String text = parser.getText().trim();
        try {
            int len = text.length();
            if (len > 9) {
                return rangeCheckedInteger(parser, context);
            }
            if (len == 0) {
                return null;
            }
            return Integer.valueOf(NumberInput.parseInt(text));
        } catch (IllegalArgumentException iae) {
            throw context.weirdStringException(_valueClass, "not a valid Integer value");//NOSONAR
        }
    }
    if (t == JsonToken.VALUE_NULL) {
        return null;
    }
    // Otherwise, no can do:
    throw context.mappingException(_valueClass);
}
 
Example #10
Source File: ParserMinimalBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getValueAsInt(int defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return getIntValue();
    }
    if (t == JsonToken.VALUE_NUMBER_FLOAT) {
        return getIntValue();
    }
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0;
            }
            return NumberInput.parseAsInt(str, defaultValue);
        case ID_TRUE:
            return 1;
        case ID_FALSE:
            return 0;
        case ID_NULL:
            return 0;
        case ID_EMBEDDED_OBJECT:
            Object value = this.getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).intValue();
            }
        }
    }
    return defaultValue;
}
 
Example #11
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method for encapsulating calls to low-level double value parsing; single place
 * just because we need a work-around that must be applied to all calls.
 */
protected final static double parseDouble(String numStr) throws NumberFormatException
{
    // avoid some nasty float representations... but should it be MIN_NORMAL or MIN_VALUE?
    if (NumberInput.NASTY_SMALL_DOUBLE.equals(numStr)) {
        return Double.MIN_NORMAL; // since 2.7; was MIN_VALUE prior
    }
    return Double.parseDouble(numStr);
}
 
Example #12
Source File: ParserBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @since 2.6
 */
protected int _parseIntValue() throws IOException
{
    // Inlined variant of: _parseNumericValue(NR_INT)

    if (_currToken == JsonToken.VALUE_NUMBER_INT) {
        char[] buf = _textBuffer.getTextBuffer();
        int offset = _textBuffer.getTextOffset();
        int len = _intLength;
        if (_numberNegative) {
            ++offset;
        }
        if (len <= 9) {
            int i = NumberInput.parseInt(buf, offset, len);
            if (_numberNegative) {
                i = -i;
            }
            _numberInt = i;
            _numTypesValid = NR_INT;
            return i;
        }
    }
    _parseNumericValue(NR_INT);
    if ((_numTypesValid & NR_INT) == 0) {
        convertNumberToInt();
    }
    return _numberInt;
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
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 32-bit int,
 * of at most 9 digits (and possible leading minus sign).
 *
 * @param neg Whether contents start with a minus sign
 *
 * @since 2.9
 */
public int contentsAsInt(boolean neg) {
    if ((_inputStart >= 0) && (_inputBuffer != null)) {
        if (neg) {
            return -NumberInput.parseInt(_inputBuffer, _inputStart+1, _inputLen-1);
        }
        return NumberInput.parseInt(_inputBuffer, _inputStart, _inputLen);
    }
    if (neg) {
        return -NumberInput.parseInt(_currentSegment, 1, _currentSize-1);
    }
    return NumberInput.parseInt(_currentSegment, 0, _currentSize);
}
 
Example #18
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
protected Byte _parseByte(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();
    Integer value = null;
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
        value = jp.getIntValue();
    }
    else if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
        String text = jp.getText().trim();
        try {
            int len = text.length();
            if (len == 0) {
                return getEmptyValue();
            }
            value = NumberInput.parseInt(text);
        } catch (IllegalArgumentException iae) {
            throw ctxt.weirdStringException(_valueClass, "not a valid Byte value");//NOSONAR
        }
    }
    if (value != null) {
        // So far so good: but does it fit?
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            throw ctxt.weirdStringException(_valueClass, "overflow, value can not be represented as 8-bit value");
        }
        return (byte) (int) value;
    }
    if (t == JsonToken.VALUE_NULL) {
        return getNullValue();
    }
    throw ctxt.mappingException(_valueClass, t);
}
 
Example #19
Source File: ParserMinimalBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public double getValueAsDouble(double defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0L;
            }
            return NumberInput.parseAsDouble(str, defaultValue);
        case ID_NUMBER_INT:
        case ID_NUMBER_FLOAT:
            return getDoubleValue();
        case ID_TRUE:
            return 1.0;
        case ID_FALSE:
        case ID_NULL:
            return 0.0;
        case ID_EMBEDDED_OBJECT:
            Object value = this.getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).doubleValue();
            }
        }
    }
    return defaultValue;
}
 
Example #20
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 #21
Source File: NumberDeserializers.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected final Long _parseLong(JsonParser p, DeserializationContext ctxt) throws IOException
{
    switch (p.getCurrentTokenId()) {
    // NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
    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:
        String text = p.getText().trim();
        if (text.length() == 0) {
            return (Long) _coerceEmptyString(ctxt, _primitive);
        }
        if (_hasTextualNull(text)) {
            return (Long) _coerceTextualNull(ctxt, _primitive);
        }
        _verifyStringForScalarCoercion(ctxt, text);
        // let's allow Strings to be converted too
        try {
            return Long.valueOf(NumberInput.parseLong(text));
        } catch (IllegalArgumentException iae) { }
        return (Long) ctxt.handleWeirdStringValue(_valueClass, text,
                "not a valid Long value");
        // fall-through
    case JsonTokenId.ID_NULL:
        return (Long) _coerceNullToken(ctxt, _primitive);
    case JsonTokenId.ID_START_ARRAY:
        return _deserializeFromArray(p, ctxt);
    }
    // Otherwise, no can do:
    return (Long) ctxt.handleUnexpectedToken(_valueClass, p);
}
 
Example #22
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 #23
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 #24
Source File: ParserMinimalBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getValueAsInt(int defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if ((t == JsonToken.VALUE_NUMBER_INT) || (t == JsonToken.VALUE_NUMBER_FLOAT)) {
        return getIntValue();
    }
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0;
            }
            return NumberInput.parseAsInt(str, defaultValue);
        case ID_TRUE:
            return 1;
        case ID_FALSE:
            return 0;
        case ID_NULL:
            return 0;
        case ID_EMBEDDED_OBJECT:
            Object value = getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).intValue();
            }
        }
    }
    return defaultValue;
}
 
Example #25
Source File: ParserMinimalBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getValueAsLong(long defaultValue) throws IOException
{
    JsonToken t = _currToken;
    if ((t == JsonToken.VALUE_NUMBER_INT) || (t == JsonToken.VALUE_NUMBER_FLOAT)) {
        return getLongValue();
    }
    if (t != null) {
        switch (t.id()) {
        case ID_STRING:
            String str = getText();
            if (_hasTextualNull(str)) {
                return 0L;
            }
            return NumberInput.parseAsLong(str, defaultValue);
        case ID_TRUE:
            return 1L;
        case ID_FALSE:
        case ID_NULL:
            return 0L;
        case ID_EMBEDDED_OBJECT:
            Object value = getEmbeddedObject();
            if (value instanceof Number) {
                return ((Number) value).longValue();
            }
        }
    }
    return defaultValue;
}
 
Example #26
Source File: OptimizedSettableBeanProperty.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
protected final int _deserializeInt(JsonParser p, DeserializationContext ctxt)
    throws IOException
{
    if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
        return p.getIntValue();
    }
    JsonToken t = p.currentToken();
    if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
        _verifyScalarCoercion(ctxt, p, "int");
        String text = p.getText().trim();
        if (_hasTextualNull(text)) {
            return 0;
        }
        try {
            int len = text.length();
            if (len > 9) {
                long l = Long.parseLong(text);
                if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
                    throw ctxt.weirdStringException(text, Integer.TYPE,
                        "Overflow: numeric value ("+text+") out of range of int ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")");
                }
                return (int) l;
            }
            if (len == 0) {
                return 0;
            }
            return NumberInput.parseInt(text);
        } catch (IllegalArgumentException iae) {
            throw ctxt.weirdStringException(text, Integer.TYPE, "not a valid int value");
        }
    }
    if (t == JsonToken.VALUE_NUMBER_FLOAT) {
        if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
            _failDoubleToIntCoercion(p, ctxt, "int");
        }
        return p.getValueAsInt();
    }
    if (t == JsonToken.VALUE_NULL) {
        if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
            _failNullToPrimitiveCoercion(ctxt, "int");
        }
        return 0;
    }
    if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
        p.nextToken();
        final int parsed = _deserializeInt(p, ctxt);
        t = p.nextToken();
        if (t != JsonToken.END_ARRAY) {
            _handleMissingEndArrayForSingle(p, ctxt);
        }            
        return parsed;            
    }
    // Otherwise, no can do:
    return (Integer) ctxt.handleUnexpectedToken(getType(), p);
}
 
Example #27
Source File: TextNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public double asDouble(double defaultValue) {
    return NumberInput.parseAsDouble(_value, defaultValue);
}
 
Example #28
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");
}
 
Example #29
Source File: TextNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public long asLong(long defaultValue) {
    return NumberInput.parseAsLong(_value, defaultValue);
}
 
Example #30
Source File: TextNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int asInt(int defaultValue) {
    return NumberInput.parseAsInt(_value, defaultValue);
}