Search in sources :

Example 46 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncNonStdNumbersTest method _testAllowNaN.

private void _testAllowNaN(JsonFactory f, String doc, int readBytes) throws Exception {
    AsyncReaderWrapper p = createParser(f, doc, readBytes);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    double d = p.getDoubleValue();
    assertTrue(Double.isNaN(d));
    assertEquals("NaN", p.currentText());
    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 BigDecimal");
    }
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
    // finally, should also work with skipping
    f = f.rebuild().enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS).build();
    p = createParser(f, doc, readBytes);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 47 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncNonStdNumbersTest method _testDisallowInf.

private void _testDisallowInf(JsonFactory f, String token, int readBytes) throws Exception {
    assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS));
    final String JSON = String.format("[%s]", token);
    // without enabling, should get an exception
    AsyncReaderWrapper p = createParser(f, JSON, readBytes);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    try {
        JsonToken t = p.nextToken();
        fail("Expected exception; got " + t + " (text [" + p.currentText() + "])");
    } catch (Exception e) {
        verifyException(e, "Non-standard token '" + token + "'");
    } finally {
        p.close();
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) IOException(java.io.IOException)

Example 48 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncNumberCoercionTest method testToIntCoercion.

/*
    /**********************************************************
    /* Numeric coercions, integral
    /**********************************************************
     */
public void testToIntCoercion() throws Exception {
    AsyncReaderWrapper p;
    // long->int
    p = createParser("1");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(1L, p.getLongValue());
    assertEquals(1, p.getIntValue());
    p.close();
    // BigInteger->int
    p = createParser("10");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.TEN, p.getBigIntegerValue());
    assertEquals(10, p.getIntValue());
    p.close();
    // double->int
    p = createParser("2");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(2.0, p.getDoubleValue());
    assertEquals(2, p.getIntValue());
    p.close();
    p = createParser("0.1");
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(0.1, p.getDoubleValue());
    assertEquals(0, p.getIntValue());
    p.close();
    // BigDecimal->int
    p = createParser("10");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigDecimal.TEN, p.getDecimalValue());
    assertEquals(10, p.getIntValue());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 49 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncNumberCoercionTest method testToIntFailing.

public void testToIntFailing() throws Exception {
    AsyncReaderWrapper p;
    // long -> error
    long big = 1L + Integer.MAX_VALUE;
    p = createParser(String.valueOf(big));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(big, p.getLongValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
    long small = -1L + Integer.MIN_VALUE;
    p = createParser(String.valueOf(small));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(Long.valueOf(small), p.getNumberValue());
    assertEquals(small, p.getLongValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
    // double -> error
    p = createParser(String.valueOf(big) + ".0");
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals((double) big, p.getDoubleValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
    p = createParser(String.valueOf(small) + ".0");
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals((double) small, p.getDoubleValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
    // BigInteger -> error
    p = createParser(String.valueOf(big));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.valueOf(big), p.getBigIntegerValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
    p = createParser(String.valueOf(small));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.valueOf(small), p.getBigIntegerValue());
    try {
        p.getIntValue();
        fail("Should not pass");
    } catch (JsonParseException e) {
        verifyException(e, "out of range of int");
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 50 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncNumberCoercionTest method testToBigIntegerCoercion.

public void testToBigIntegerCoercion() throws Exception {
    AsyncReaderWrapper p;
    p = createParser("1");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    // int to BigInteger
    assertEquals(1, p.getIntValue());
    assertEquals(BigInteger.ONE, p.getBigIntegerValue());
    p.close();
    p = createParser("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(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(" 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();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) BigDecimal(java.math.BigDecimal)

Aggregations

AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)87 Test (org.junit.Test)10 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)5 BigDecimal (java.math.BigDecimal)4 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 NonBlockingJsonParserBase (com.fasterxml.jackson.core.json.async.NonBlockingJsonParserBase)2 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2