use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class ValueConversionsTest method _testAsLong.
public void _testAsLong(int mode) throws Exception {
final String input = "[ 1, -3, 4.98, true, false, null, \"-17\", \"foo\" ]";
JsonParser p = createParser(mode, input);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertEquals(0L, p.getValueAsLong());
assertEquals(9L, p.getValueAsLong(9L));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1L, p.getValueAsLong());
assertEquals(1L, p.getValueAsLong(-99L));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(-3L, p.getValueAsLong());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(4L, p.getValueAsLong());
assertEquals(4L, p.getValueAsLong(99L));
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertEquals(1L, p.getValueAsLong());
assertToken(JsonToken.VALUE_FALSE, p.nextToken());
assertEquals(0L, p.getValueAsLong());
assertToken(JsonToken.VALUE_NULL, p.nextToken());
assertEquals(0L, p.getValueAsLong());
assertEquals(0L, p.getValueAsLong(27L));
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(-17L, p.getValueAsLong());
assertEquals(-17L, p.getValueAsLong(3L));
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(0L, p.getValueAsLong());
assertEquals(9L, p.getValueAsLong(9L));
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertEquals(0L, p.getValueAsLong());
assertEquals(9L, p.getValueAsLong(9L));
p.close();
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class NumberCoercionTest method testToLongCoercion.
public void testToLongCoercion() throws Exception {
for (int mode : ALL_STREAMING_MODES) {
JsonParser p;
// int->long
p = createParser(mode, "1");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getIntValue());
assertEquals(1L, p.getLongValue());
p.close();
// BigInteger->long
long biggish = 12345678901L;
p = createParser(mode, String.valueOf(biggish));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigInteger.valueOf(biggish), p.getBigIntegerValue());
assertEquals(biggish, p.getLongValue());
p.close();
// double->long
p = createParser(mode, "2");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(2.0, p.getDoubleValue());
assertEquals(2L, p.getLongValue());
p.close();
// BigDecimal->long
p = createParser(mode, "10");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigDecimal.TEN, p.getDecimalValue());
assertEquals(10, p.getLongValue());
p.close();
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class NumberCoercionTest method testToIntCoercion.
/*
/**********************************************************
/* Numeric coercions, integral
/**********************************************************
*/
public void testToIntCoercion() throws Exception {
for (int mode : ALL_STREAMING_MODES) {
JsonParser p;
// long->int
p = createParser(mode, "1");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1L, p.getLongValue());
assertEquals(1, p.getIntValue());
p.close();
// BigInteger->int
p = createParser(mode, "10");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigInteger.TEN, p.getBigIntegerValue());
assertEquals(10, p.getIntValue());
p.close();
// double->int
p = createParser(mode, "2");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(2.0, p.getDoubleValue());
assertEquals(2, p.getIntValue());
p.close();
// BigDecimal->int
p = createParser(mode, "10");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigDecimal.TEN, p.getDecimalValue());
assertEquals(10, p.getIntValue());
p.close();
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class NumberCoercionTest method testToDoubleCoercion.
/*
/**********************************************************
/* Numeric coercions, floating point
/**********************************************************
*/
public void testToDoubleCoercion() throws Exception {
for (int mode : ALL_STREAMING_MODES) {
JsonParser p;
// BigDecimal->double
p = createParser(mode, "100.5");
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(new BigDecimal("100.5"), p.getDecimalValue());
assertEquals(100.5, p.getDoubleValue());
p.close();
p = createParser(mode, "10");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(BigInteger.TEN, p.getBigIntegerValue());
assertEquals(10.0, p.getDoubleValue());
p.close();
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class NumberCoercionTest method testToLongFailing.
@SuppressWarnings("resource")
public void testToLongFailing() throws Exception {
for (int mode : ALL_STREAMING_MODES) {
JsonParser p;
// BigInteger -> error
BigInteger big = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN);
p = createParser(mode, String.valueOf(big));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.BIG_INTEGER, p.getNumberType());
assertEquals(big, p.getBigIntegerValue());
assertEquals(big, p.getNumberValue());
try {
p.getLongValue();
fail("Should not pass");
} catch (JsonParseException e) {
verifyException(e, "out of range of long");
}
BigInteger small = BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN);
p = createParser(mode, String.valueOf(small));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(small, p.getBigIntegerValue());
try {
p.getLongValue();
fail("Should not pass");
} catch (JsonParseException e) {
verifyException(e, "out of range of long");
}
}
}
Aggregations