use of 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");
}
}
}
use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class NumberCoercionTest method testToBigIntegerCoercion.
public void testToBigIntegerCoercion() throws Exception {
for (int mode : ALL_STREAMING_MODES) {
JsonParser p;
p = createParser(mode, "1");
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
// int to BigInteger
assertEquals(1, p.getIntValue());
assertEquals(BigInteger.ONE, p.getBigIntegerValue());
p.close();
p = createParser(mode, "2.0");
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
// double to BigInteger
assertEquals(2.0, p.getDoubleValue());
assertEquals(BigInteger.valueOf(2L), p.getBigIntegerValue());
p.close();
p = createParser(mode, String.valueOf(Long.MAX_VALUE));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
// long to BigInteger
assertEquals(Long.MAX_VALUE, p.getLongValue());
assertEquals(BigInteger.valueOf(Long.MAX_VALUE), p.getBigIntegerValue());
p.close();
p = createParser(mode, " 200.0");
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
// BigDecimal to BigInteger
assertEquals(new BigDecimal("200.0"), p.getDecimalValue());
assertEquals(BigInteger.valueOf(200L), p.getBigIntegerValue());
p.close();
}
}
use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class TrailingCommasTest method testObjectTrailingCommas.
@Test
public void testObjectTrailingCommas() throws Exception {
String json = "{\"a\": true, \"b\": false,,}";
JsonParser p = createParser(factory, mode, json);
assertEquals(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.getText());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("b", p.getText());
assertToken(JsonToken.VALUE_FALSE, p.nextToken());
assertUnexpected(p, ',');
p.close();
}
use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class TrailingCommasTest method testArrayTrailingComma.
@Test
public void testArrayTrailingComma() throws Exception {
String json = "[\"a\", \"b\",]";
JsonParser p = createParser(factory, mode, json);
assertEquals(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("a", p.getText());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("b", p.getText());
// ALLOW_TRAILING_COMMA takes priority over ALLOW_MISSING_VALUES
if (features.contains(Feature.ALLOW_TRAILING_COMMA)) {
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertEnd(p);
} else if (features.contains(Feature.ALLOW_MISSING_VALUES)) {
assertToken(JsonToken.VALUE_NULL, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertEnd(p);
} else {
assertUnexpected(p, ']');
}
p.close();
}
use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.
the class TrailingCommasTest method testObjectInnerComma.
@Test
public void testObjectInnerComma() throws Exception {
String json = "{\"a\": true,, \"b\": false}";
JsonParser p = createParser(factory, mode, json);
assertEquals(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.getText());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertUnexpected(p, ',');
p.close();
}
Aggregations