Search in sources :

Example 46 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project eol-globi-data by jhpoelen.

the class ResultFormatterSeparatedValues method handleRows.

private void handleRows(OutputStream os, JsonParser jsonParser) throws IOException {
    JsonToken token;
    token = jsonParser.nextToken();
    if (START_ARRAY.equals(token)) {
        boolean isFirstValue = true;
        boolean inValueArray = false;
        boolean inRowArray = false;
        String currentFieldName = null;
        ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream();
        while ((token = jsonParser.nextToken()) != null) {
            if (FIELD_NAME.equals(token)) {
                currentFieldName = jsonParser.getCurrentName();
            }
            if (START_ARRAY.equals(token)) {
                if (inRowArray && !StringUtils.equals("meta", currentFieldName)) {
                    inValueArray = true;
                } else {
                    isFirstValue = true;
                    inRowArray = true;
                }
            } else if (isValue(token)) {
                if (!isFirstValue && !inValueArray) {
                    if (!StringUtils.equals("meta", currentFieldName)) {
                        IOUtils.write(getFieldSeparator(), lineBuffer, StandardCharsets.UTF_8);
                    }
                }
                if (inValueArray) {
                    if (lineBuffer.size() > 0) {
                        lineBuffer.writeTo(os);
                        IOUtils.write(getFieldSeparator(), os, StandardCharsets.UTF_8);
                        writeValue(os, jsonParser, token);
                        addNewline(os);
                    }
                } else {
                    writeValue(lineBuffer, jsonParser, token);
                }
                isFirstValue = false;
            } else if (END_ARRAY.equals(token)) {
                if (inValueArray) {
                    inValueArray = false;
                } else if (inRowArray) {
                    if (lineBuffer.size() > 0) {
                        addNewline(lineBuffer);
                        lineBuffer.writeTo(os);
                    }
                    inRowArray = false;
                }
                lineBuffer = new ByteArrayOutputStream();
            }
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project eol-globi-data by jhpoelen.

the class ResultFormatterSeparatedValues method format.

@Override
public void format(InputStream is, OutputStream os) throws ResultFormattingException {
    try (InputStream inputStream = is) {
        JsonFactory factory = new JsonFactory();
        JsonParser jsonParser = factory.createJsonParser(inputStream);
        JsonToken token;
        while (!jsonParser.isClosed() && (token = jsonParser.nextToken()) != null) {
            if (FIELD_NAME.equals(token) && "columns".equals(jsonParser.getCurrentName())) {
                handleHeader(os, jsonParser);
            } else if (FIELD_NAME.equals(token) && "data".equals(jsonParser.getCurrentName())) {
                handleRows(os, jsonParser);
            }
        }
    } catch (IOException e) {
        throw new ResultFormattingException("failed to format incoming stream", e);
    }
}
Also used : InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 48 with JsonToken

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project eol-globi-data by jhpoelen.

the class ResultFormatterSeparatedValues method handleHeader.

public void handleHeader(OutputStream os, JsonParser jsonParser) throws IOException {
    JsonToken token;
    token = jsonParser.nextToken();
    if (START_ARRAY.equals(token)) {
        boolean isFirstValue = true;
        while ((token = jsonParser.nextToken()) != null && !END_ARRAY.equals(token)) {
            if (isValue(token)) {
                if (!isFirstValue) {
                    IOUtils.write(getFieldSeparator(), os, StandardCharsets.UTF_8);
                }
                writeValue(os, jsonParser, token);
                isFirstValue = false;
            }
        }
        addNewline(os);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 49 with JsonToken

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

the class TypeSpecificMarshaller method mergeValue.

void mergeValue(JsonParser parser, int currentDepth, Message.Builder builder) throws IOException {
    ParseSupport.checkRecursionLimit(currentDepth);
    JsonToken json = parser.currentToken();
    if (json == null) {
        // Nested messages will already have current token set, but top-level ones will not.
        json = parser.nextToken();
    }
    if (json != JsonToken.START_OBJECT) {
        throw new InvalidProtocolBufferException("Expected start of object, got: " + parser.getText());
    }
    doMerge(parser, currentDepth, builder);
// Object end will be handled in ParseSupport.checkObjectEnd.
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 50 with JsonToken

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

the class ParseSupport method parseArrayStart.

/**
 * Checks whether the current token is '[' and advances past it.
 */
public static void parseArrayStart(JsonParser parser) throws IOException {
    JsonToken json = parser.currentToken();
    if (json != JsonToken.START_ARRAY) {
        throw new InvalidProtocolBufferException("Expected an array but found: " + json);
    }
    parser.nextToken();
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonToken(com.fasterxml.jackson.core.JsonToken)

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