Search in sources :

Example 51 with AsyncReaderWrapper

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

the class AsyncNumberCoercionTest method testToDoubleCoercion.

/*
    /**********************************************************
    /* Numeric coercions, floating point
    /**********************************************************
     */
public void testToDoubleCoercion() throws Exception {
    AsyncReaderWrapper p;
    // BigDecimal->double
    p = createParser("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("10");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigInteger.TEN, p.getBigIntegerValue());
    assertEquals(10.0, p.getDoubleValue());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) BigDecimal(java.math.BigDecimal)

Example 52 with AsyncReaderWrapper

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

the class AsyncNumberCoercionTest method testToBigDecimalCoercion.

public void testToBigDecimalCoercion() throws Exception {
    AsyncReaderWrapper p;
    p = createParser("1");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    // int to BigDecimal
    assertEquals(1, p.getIntValue());
    assertEquals(BigDecimal.ONE, p.getDecimalValue());
    p.close();
    p = createParser(String.valueOf(Long.MAX_VALUE));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    // long to BigDecimal
    assertEquals(Long.MAX_VALUE, p.getLongValue());
    assertEquals(BigDecimal.valueOf(Long.MAX_VALUE), p.getDecimalValue());
    p.close();
    BigInteger biggie = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.TEN);
    p = createParser(String.valueOf(biggie));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    // BigInteger to BigDecimal
    assertEquals(biggie, p.getBigIntegerValue());
    assertEquals(new BigDecimal(biggie), p.getDecimalValue());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 53 with AsyncReaderWrapper

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

the class AsyncNumberCoercionTest method testToLongCoercion.

public void testToLongCoercion() throws Exception {
    AsyncReaderWrapper p;
    // int->long
    p = createParser("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(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("2");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(2.0, p.getDoubleValue());
    assertEquals(2L, p.getLongValue());
    p.close();
    // BigDecimal->long
    p = createParser("10");
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(BigDecimal.TEN, p.getDecimalValue());
    assertEquals(10, p.getLongValue());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 54 with AsyncReaderWrapper

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

the class AsyncParserNamesTest method _testWithName.

private void _testWithName(String name) throws IOException {
    byte[] doc = _jsonDoc("{" + quote(name) + ":13}");
    AsyncReaderWrapper p = asyncForBytes(JSON_F, 37, doc, 0);
    assertNull(p.currentToken());
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    assertToken(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals(name, p.currentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(13, p.getIntValue());
    assertEquals(name, p.currentName());
    assertToken(JsonToken.END_OBJECT, p.nextToken());
    assertNull(p.nextToken());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 55 with AsyncReaderWrapper

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

the class AsyncParserNamesTest method testSymbolTable.

public void testSymbolTable() throws IOException {
    final String STR1 = "a";
    byte[] doc = _jsonDoc("{ " + quote(STR1) + ":1, \"foobar\":2, \"longername\":3 }");
    JsonFactory f = JSON_F;
    AsyncReaderWrapper p = asyncForBytes(f, 5, doc, 0);
    final ByteQuadsCanonicalizer symbols1 = ((NonBlockingJsonParserBase) p.parser()).symbolTableForTests();
    assertEquals(0, symbols1.size());
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    // field names not intern()ed by default any more in 3.x
    assertEquals(STR1, p.currentName());
    // assertSame(STR1, p.currentName());
    assertEquals(1, symbols1.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("foobar", p.currentName());
    assertEquals(2, symbols1.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("longername", p.currentName());
    assertEquals(3, symbols1.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.END_OBJECT, p.nextToken());
    assertNull(p.nextToken());
    assertEquals(3, symbols1.size());
    p.close();
    // but let's verify that symbol table gets reused properly
    p = asyncForBytes(f, 5, doc, 0);
    final ByteQuadsCanonicalizer symbols2 = ((NonBlockingJsonParserBase) p.parser()).symbolTableForTests();
    // symbol tables are not reused, but contents are:
    assertNotSame(symbols1, symbols2);
    assertEquals(3, symbols2.size());
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    // field names are interned:
    assertEquals(STR1, p.currentName());
    assertEquals(3, symbols2.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("foobar", p.currentName());
    assertEquals(3, symbols2.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("longername", p.currentName());
    assertEquals(3, symbols2.size());
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.END_OBJECT, p.nextToken());
    assertNull(p.nextToken());
    assertEquals(3, symbols2.size());
    p.close();
    assertEquals(3, symbols2.size());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) JsonFactory(com.fasterxml.jackson.core.json.JsonFactory) ByteQuadsCanonicalizer(com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer) NonBlockingJsonParserBase(com.fasterxml.jackson.core.json.async.NonBlockingJsonParserBase)

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