Search in sources :

Example 16 with StreamReadException

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();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException)

Example 17 with StreamReadException

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();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException)

Example 18 with StreamReadException

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 + "\"");
}
Also used : StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException)

Example 19 with StreamReadException

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();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 20 with StreamReadException

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();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)68 AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)23 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)11 CBORParser (com.fasterxml.jackson.dataformat.cbor.CBORParser)9 JsonToken (com.fasterxml.jackson.core.JsonToken)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayFeeder (com.fasterxml.jackson.core.async.ByteArrayFeeder)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URL (java.net.URL)2 Test (org.junit.Test)2 InputCoercionException (com.fasterxml.jackson.core.exc.InputCoercionException)1 WrappedIOException (com.fasterxml.jackson.core.exc.WrappedIOException)1 FilteringParserDelegate (com.fasterxml.jackson.core.filter.FilteringParserDelegate)1 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 SmileGenerator (com.fasterxml.jackson.dataformat.smile.SmileGenerator)1 SmileMapper (com.fasterxml.jackson.dataformat.smile.databind.SmileMapper)1 ThrottledInputStream (com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream)1