Search in sources :

Example 61 with JsonToken

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

the class ParserErrorHandlingTest method _testMangledNumbersInt.

private void _testMangledNumbersInt(int mode) throws Exception {
    JsonParser p = createParser(mode, "123true");
    try {
        JsonToken t = p.nextToken();
        fail("Should have gotten an exception; instead got token: " + t);
    } catch (JsonParseException e) {
        verifyException(e, "expected space");
    }
    p.close();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 62 with JsonToken

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

the class UTF8DataInputJsonParserTest method test_parsePosNumber.

@Test
public void test_parsePosNumber() throws IOException {
    byte[] byteArray = new byte[2];
    byteArray[0] = (byte) 51;
    byteArray[1] = (byte) 22;
    IOContext ioContext = new IOContext(new BufferRecycler(), byteArray, false);
    InputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    ByteQuadsCanonicalizer byteQuadsCanonicalizer = ByteQuadsCanonicalizer.createRoot();
    DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
    UTF8DataInputJsonParser uTF8DataInputJsonParser = new UTF8DataInputJsonParser(ObjectReadContext.empty(), ioContext, 1568, dataInputStream, byteQuadsCanonicalizer, 13);
    JsonToken jsonToken = uTF8DataInputJsonParser._parsePosNumber(7);
    assertEquals(7, jsonToken.id());
    assertNull(jsonToken.asString());
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext) ByteQuadsCanonicalizer(com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer) JsonToken(com.fasterxml.jackson.core.JsonToken) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Example 63 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project syndesis by syndesisio.

the class JsonRecordSupport method jsonStreamToRecords.

public static void jsonStreamToRecords(Set<String> indexes, String dbPath, InputStream is, Consumer<JsonRecord> consumer) throws IOException {
    try (JsonParser jp = new JsonFactory().createParser(is)) {
        jsonStreamToRecords(indexes, jp, dbPath, consumer);
        JsonToken jsonToken = jp.nextToken();
        if (jsonToken != null) {
            throw new JsonParseException(jp, "Document did not terminate as expected.");
        }
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 64 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project debezium by debezium.

the class JacksonReader method parseDocument.

private Document parseDocument(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the fields in the top-level document ...
    BasicDocument doc = new BasicDocument();
    JsonToken token = null;
    if (!nested) {
        // We expect the START_OBJECT token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object, but was " + token);
        }
    }
    String fieldName = null;
    token = parser.nextToken();
    while (token != JsonToken.END_OBJECT) {
        switch(token) {
            case FIELD_NAME:
                fieldName = parser.getCurrentName();
                break;
            case START_OBJECT:
                doc.setDocument(fieldName, parseDocument(parser, true));
                break;
            case START_ARRAY:
                doc.setArray(fieldName, parseArray(parser, true));
                break;
            case VALUE_STRING:
                doc.setString(fieldName, parser.getValueAsString());
                break;
            case VALUE_TRUE:
                doc.setBoolean(fieldName, true);
                break;
            case VALUE_FALSE:
                doc.setBoolean(fieldName, false);
                break;
            case VALUE_NULL:
                doc.setNull(fieldName);
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                switch(parser.getNumberType()) {
                    case FLOAT:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getFloatValue());
                        }
                        break;
                    case DOUBLE:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getDoubleValue());
                        }
                        break;
                    case BIG_DECIMAL:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getDecimalValue());
                        }
                        break;
                    case INT:
                        doc.setNumber(fieldName, parser.getIntValue());
                        break;
                    case LONG:
                        doc.setNumber(fieldName, parser.getLongValue());
                        break;
                    case BIG_INTEGER:
                        doc.setNumber(fieldName, parser.getBigIntegerValue());
                        break;
                }
                break;
            case VALUE_EMBEDDED_OBJECT:
                // disregard this, since it's an extension ...
                break;
            case NOT_AVAILABLE:
                throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation());
            case END_ARRAY:
                throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation());
            case END_OBJECT:
                throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return doc;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 65 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project debezium by debezium.

the class JacksonReader method parseArray.

private Array parseArray(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the values in the array ...
    BasicArray array = new BasicArray();
    JsonToken token = null;
    if (!nested) {
        // We expect the START_ARRAY token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_ARRAY) {
            throw new IOException("Expected data to start with an Array, but was " + token);
        }
    }
    token = parser.nextToken();
    while (token != JsonToken.END_ARRAY) {
        switch(token) {
            case START_OBJECT:
                array.add(parseDocument(parser, true));
                break;
            case START_ARRAY:
                array.add(parseArray(parser, true));
                break;
            case VALUE_STRING:
                array.add(parser.getValueAsString());
                break;
            case VALUE_TRUE:
                array.add(true);
                break;
            case VALUE_FALSE:
                array.add(false);
                break;
            case VALUE_NULL:
                array.addNull();
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                switch(parser.getNumberType()) {
                    case FLOAT:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getFloatValue());
                        }
                        break;
                    case DOUBLE:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getDoubleValue());
                        }
                        break;
                    case BIG_DECIMAL:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getDecimalValue());
                        }
                        break;
                    case INT:
                        array.add(parser.getIntValue());
                        break;
                    case LONG:
                        array.add(parser.getLongValue());
                        break;
                    case BIG_INTEGER:
                        array.add(parser.getBigIntegerValue());
                        break;
                }
                break;
            case VALUE_EMBEDDED_OBJECT:
                // disregard this, since it's an extension ...
                break;
            case NOT_AVAILABLE:
                throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation());
            case FIELD_NAME:
                throw new JsonParseException(parser, "Not expecting a FIELD_NAME token", parser.getCurrentLocation());
            case END_ARRAY:
                throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation());
            case END_OBJECT:
                throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return array;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)251 IOException (java.io.IOException)65 JsonParser (com.fasterxml.jackson.core.JsonParser)44 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 JsonFactory (com.fasterxml.jackson.core.JsonFactory)18 JsonParseException (com.fasterxml.jackson.core.JsonParseException)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 SqlNullable (com.facebook.presto.spi.function.SqlNullable)6 SqlType (com.facebook.presto.spi.function.SqlType)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 InputStream (java.io.InputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ByteString (com.google.protobuf.ByteString)4 HashSet (java.util.HashSet)4 MappingJsonFactory (com.fasterxml.jackson.databind.MappingJsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3