use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-core by FasterXML.
the class NonStandardJsonReadFeaturesTest method _testAllowInf.
private void _testAllowInf(int mode) {
final String JSON = "[ -INF, +INF, +Infinity, Infinity, -Infinity ]";
// without enabling, should get an exception
JsonParser p = createParser(STD_F, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (StreamReadException e) {
verifyException(e, "Non-standard token '-INF'");
} finally {
p.close();
}
JsonFactory f = JsonFactory.builder().enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
double d = p.getDoubleValue();
assertEquals("-INF", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.NEGATIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("+INF", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("+Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("-Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.NEGATIVE_INFINITY);
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// should also work with skipping
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-core by FasterXML.
the class NonStandardJsonReadFeaturesTest method _testAllowNaN.
private void _testAllowNaN(int mode) {
final String JSON = "[ NaN]";
// without enabling, should get an exception
JsonParser p = createParser(STD_F, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (StreamReadException e) {
verifyException(e, "non-standard");
} finally {
p.close();
}
// we can enable it dynamically (impl detail)
JsonFactory f = JsonFactory.builder().enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
double d = p.getDoubleValue();
assertTrue(Double.isNaN(d));
assertEquals("NaN", p.getText());
// [Issue#98]
try {
/*BigDecimal dec =*/
p.getDecimalValue();
fail("Should fail when trying to access NaN as BigDecimal");
} catch (NumberFormatException e) {
verifyException(e, "can not be represented as `java.math.BigDecimal`");
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// finally, should also work with skipping
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-core by FasterXML.
the class ParserDupHandlingTest method _testSimpleDupsFail.
private void _testSimpleDupsFail(final String doc, JsonFactory f, int mode, String name) {
JsonParser p = createParser(f, mode, doc);
JsonToken t = p.nextToken();
assertNotNull(t);
assertTrue(t.isStructStart());
int depth = 1;
StreamReadException e = null;
while (depth > 0) {
try {
switch(p.nextToken()) {
case START_ARRAY:
case START_OBJECT:
++depth;
break;
case END_ARRAY:
case END_OBJECT:
--depth;
break;
default:
}
} catch (StreamReadException e0) {
e = e0;
break;
}
}
p.close();
if (e == null) {
fail("Should have caught exception for dup");
}
verifyException(e, "duplicate Object Property \"" + name + "\"");
}
use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-core by FasterXML.
the class ParserErrorHandlingTest method _testMangledNumbersInt.
private void _testMangledNumbersInt(int mode) {
JsonParser p = createParser(mode, "123true");
try {
JsonToken t = p.nextToken();
fail("Should have gotten an exception; instead got token: " + t);
} catch (StreamReadException e) {
verifyException(e, "expected space");
}
p.close();
}
use of com.fasterxml.jackson.core.exc.StreamReadException in project jackson-core by FasterXML.
the class ParserErrorHandlingTest method _testMangledNumbersFloat.
private void _testMangledNumbersFloat(int mode) {
// Also test with floats
JsonParser p = createParser(mode, "1.5false");
try {
JsonToken t = p.nextToken();
fail("Should have gotten an exception; instead got token: " + t);
} catch (StreamReadException e) {
verifyException(e, "expected space");
}
p.close();
}
Aggregations