Search in sources :

Example 41 with JsonParser

use of 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)

Example 42 with JsonParser

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

the class NumberCoercionTest method testToBigIntegerCoercion.

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

Example 43 with JsonParser

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

the class TrailingCommasTest method testObjectTrailingCommas.

@Test
public void testObjectTrailingCommas() throws Exception {
    String json = "{\"a\": true, \"b\": false,,}";
    JsonParser p = createParser(factory, mode, json);
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertToken(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("a", p.getText());
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertToken(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("b", p.getText());
    assertToken(JsonToken.VALUE_FALSE, p.nextToken());
    assertUnexpected(p, ',');
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) UTF8DataInputJsonParser(com.fasterxml.jackson.core.json.UTF8DataInputJsonParser) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Example 44 with JsonParser

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

the class TrailingCommasTest method testArrayTrailingComma.

@Test
public void testArrayTrailingComma() throws Exception {
    String json = "[\"a\", \"b\",]";
    JsonParser p = createParser(factory, mode, json);
    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals("a", p.getText());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals("b", p.getText());
    // ALLOW_TRAILING_COMMA takes priority over ALLOW_MISSING_VALUES
    if (features.contains(Feature.ALLOW_TRAILING_COMMA)) {
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertEnd(p);
    } else if (features.contains(Feature.ALLOW_MISSING_VALUES)) {
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertEnd(p);
    } else {
        assertUnexpected(p, ']');
    }
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) UTF8DataInputJsonParser(com.fasterxml.jackson.core.json.UTF8DataInputJsonParser) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Example 45 with JsonParser

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

the class TrailingCommasTest method testObjectInnerComma.

@Test
public void testObjectInnerComma() throws Exception {
    String json = "{\"a\": true,, \"b\": false}";
    JsonParser p = createParser(factory, mode, json);
    assertEquals(JsonToken.START_OBJECT, p.nextToken());
    assertToken(JsonToken.FIELD_NAME, p.nextToken());
    assertEquals("a", p.getText());
    assertToken(JsonToken.VALUE_TRUE, p.nextToken());
    assertUnexpected(p, ',');
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) UTF8DataInputJsonParser(com.fasterxml.jackson.core.json.UTF8DataInputJsonParser) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

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