Search in sources :

Example 16 with JsonParser

use of org.apache.flink.shaded.jackson2.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 17 with JsonParser

use of org.apache.flink.shaded.jackson2.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 18 with JsonParser

use of org.apache.flink.shaded.jackson2.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 19 with JsonParser

use of org.apache.flink.shaded.jackson2.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)

Example 20 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.

the class NumberCoercionTest method testToLongFailing.

@SuppressWarnings("resource")
public void testToLongFailing() throws Exception {
    for (int mode : ALL_STREAMING_MODES) {
        JsonParser p;
        // BigInteger -> error
        BigInteger big = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN);
        p = createParser(mode, String.valueOf(big));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(NumberType.BIG_INTEGER, p.getNumberType());
        assertEquals(big, p.getBigIntegerValue());
        assertEquals(big, p.getNumberValue());
        try {
            p.getLongValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of long");
        }
        BigInteger small = BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN);
        p = createParser(mode, String.valueOf(small));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(small, p.getBigIntegerValue());
        try {
            p.getLongValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of long");
        }
    }
}
Also used : BigInteger(java.math.BigInteger) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)587 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)258 JacksonWrapperParser (com.abubusoft.kripton.persistence.JacksonWrapperParser)258 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 ArrayList (java.util.ArrayList)171 IOException (java.io.IOException)126 JsonFactory (com.fasterxml.jackson.core.JsonFactory)76 Test (org.junit.Test)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)51 JsonNode (com.fasterxml.jackson.databind.JsonNode)46 HashSet (java.util.HashSet)43 JsonToken (com.fasterxml.jackson.core.JsonToken)41 LinkedHashSet (java.util.LinkedHashSet)38 HashMap (java.util.HashMap)35 LinkedList (java.util.LinkedList)26 JsonParseException (com.fasterxml.jackson.core.JsonParseException)23 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)21 List (java.util.List)21 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)20 InputStream (java.io.InputStream)20