Search in sources :

Example 36 with JsonParser

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

the class ValueConversionsTest method _testAsInt.

private void _testAsInt(int mode) throws Exception {
    final String input = "[ 1, -3, 4.98, true, false, null, \"-17\", \"foo\" ]";
    JsonParser p = createParser(mode, input);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertEquals(0, p.getValueAsLong());
    assertEquals(9, p.getValueAsLong(9));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(1, p.getValueAsLong());
    assertEquals(1, p.getValueAsLong(-99));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(-3, p.getValueAsLong());
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(4, p.getValueAsLong());
    assertEquals(4, p.getValueAsLong(99));
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertEquals(1, p.getValueAsLong());
    assertToken(JsonToken.VALUE_FALSE, p.nextToken());
    assertEquals(0, p.getValueAsLong());
    assertToken(JsonToken.VALUE_NULL, p.nextToken());
    assertEquals(0, p.getValueAsLong());
    assertEquals(0, p.getValueAsLong(27));
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(-17, p.getValueAsLong());
    assertEquals(-17, p.getValueAsLong(3));
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(0, p.getValueAsLong());
    assertEquals(9, p.getValueAsLong(9));
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertEquals(0, p.getValueAsLong());
    assertEquals(9, p.getValueAsLong(9));
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 37 with JsonParser

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

the class ValueConversionsTest method _testAsLong.

public void _testAsLong(int mode) throws Exception {
    final String input = "[ 1, -3, 4.98, true, false, null, \"-17\", \"foo\" ]";
    JsonParser p = createParser(mode, input);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertEquals(0L, p.getValueAsLong());
    assertEquals(9L, p.getValueAsLong(9L));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(1L, p.getValueAsLong());
    assertEquals(1L, p.getValueAsLong(-99L));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(-3L, p.getValueAsLong());
    assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
    assertEquals(4L, p.getValueAsLong());
    assertEquals(4L, p.getValueAsLong(99L));
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertEquals(1L, p.getValueAsLong());
    assertToken(JsonToken.VALUE_FALSE, p.nextToken());
    assertEquals(0L, p.getValueAsLong());
    assertToken(JsonToken.VALUE_NULL, p.nextToken());
    assertEquals(0L, p.getValueAsLong());
    assertEquals(0L, p.getValueAsLong(27L));
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(-17L, p.getValueAsLong());
    assertEquals(-17L, p.getValueAsLong(3L));
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(0L, p.getValueAsLong());
    assertEquals(9L, p.getValueAsLong(9L));
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertEquals(0L, p.getValueAsLong());
    assertEquals(9L, p.getValueAsLong(9L));
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 38 with JsonParser

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

the class NumberCoercionTest method testToLongCoercion.

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

Example 39 with JsonParser

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

the class NumberCoercionTest method testToIntCoercion.

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

Example 40 with JsonParser

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

the class NumberCoercionTest method testToDoubleCoercion.

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

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5