Search in sources :

Example 1 with InputCoercionException

use of com.fasterxml.jackson.core.exc.InputCoercionException in project curiostack by curioswitch.

the class ParseSupport method parseUInt64.

/**
 * Parsers a uint64 value out of the input.
 */
public static long parseUInt64(JsonParser parser) throws IOException {
    // cover the vast majority of cases.
    try {
        long result = parseLong(parser);
        if (result >= 0) {
            // Only need to check the uint32 range if the parsed long is negative.
            return result;
        }
    } catch (JsonParseException | InputCoercionException | NumberFormatException e) {
    // Fall through.
    }
    final BigInteger value;
    try {
        BigDecimal decimal = new BigDecimal(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
        value = decimal.toBigIntegerExact();
    } catch (ArithmeticException | NumberFormatException e) {
        throw new InvalidProtocolBufferException("Not an uint64 value: " + parser.getText());
    }
    if (value.compareTo(BigInteger.ZERO) < 0 || value.compareTo(MAX_UINT64) > 0) {
        throw new InvalidProtocolBufferException("Out of range uint64 value: " + parser.getText());
    }
    return value.longValue();
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InputCoercionException(com.fasterxml.jackson.core.exc.InputCoercionException) BigInteger(java.math.BigInteger) JsonParseException(com.fasterxml.jackson.core.JsonParseException) BigDecimal(java.math.BigDecimal)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 InputCoercionException (com.fasterxml.jackson.core.exc.InputCoercionException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1