use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class ValueConversionsTest method _testAsInt.
private void _testAsInt(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(0, p.getValueAsLong());
assertEquals(9, p.getValueAsLong(9));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(1, p.getValueAsLong());
assertEquals(1, p.getValueAsLong(-99));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(-3, p.getValueAsLong());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(4, p.getValueAsLong());
assertEquals(4, p.getValueAsLong(99));
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertEquals(1, p.getValueAsLong());
assertToken(JsonToken.VALUE_FALSE, p.nextToken());
assertEquals(0, p.getValueAsLong());
assertToken(JsonToken.VALUE_NULL, p.nextToken());
assertEquals(0, p.getValueAsLong());
assertEquals(0, p.getValueAsLong(27));
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(-17, p.getValueAsLong());
assertEquals(-17, p.getValueAsLong(3));
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(0, p.getValueAsLong());
assertEquals(9, p.getValueAsLong(9));
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertEquals(0, p.getValueAsLong());
assertEquals(9, p.getValueAsLong(9));
p.close();
}
use of 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 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 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 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();
}
}
Aggregations