Search in sources :

Example 86 with JsonToken

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

the class BaseJsonProcessor method processJSONException.

/*
   * DRILL - 4653 This method processes JSON tokens until it reaches end of the
   * current line when it processes start of a new JSON line { - return
   * PROC_SUCCEED when it sees EOF the stream - there may not be a closing }
   */
protected JsonExceptionProcessingState processJSONException() throws IOException {
    while (!parser.isClosed()) {
        try {
            JsonToken currentToken = parser.nextToken();
            if (currentToken == JsonToken.START_OBJECT && (lastSeenJsonToken == JsonToken.END_OBJECT || lastSeenJsonToken == null)) {
                lastSeenJsonToken = currentToken;
                break;
            }
            lastSeenJsonToken = currentToken;
        } catch (com.fasterxml.jackson.core.JsonParseException ex1) {
            if (ex1.getOriginalMessage().startsWith(JACKSON_PARSER_EOF_FILE_MSG)) {
                return JsonExceptionProcessingState.END_OF_STREAM;
            }
            continue;
        }
    }
    return JsonExceptionProcessingState.PROC_SUCCEED;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 87 with JsonToken

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

the class BaseJsonReader method write.

@Override
public ReadState write(ComplexWriter writer) throws IOException {
    try {
        JsonToken t = lastSeenJsonToken;
        if (t == null || t == JsonToken.END_OBJECT) {
            t = parser.nextToken();
        }
        while (!parser.hasCurrentToken() && !parser.isClosed()) {
            t = parser.nextToken();
        }
        lastSeenJsonToken = null;
        if (parser.isClosed()) {
            return ReadState.END_OF_STREAM;
        }
        ReadState readState = writeToVector(writer, t);
        switch(readState) {
            case END_OF_STREAM:
            case WRITE_SUCCEED:
                return readState;
            default:
                throw getExceptionWithContext(UserException.dataReadError(), null).message("Failure while reading JSON. (Got an invalid read state %s )", readState.toString()).build(logger);
        }
    } catch (com.fasterxml.jackson.core.JsonParseException ex) {
        if (ignoreJSONParseError()) {
            if (processJSONException() == JsonExceptionProcessingState.END_OF_STREAM) {
                return ReadState.JSON_RECORD_PARSE_EOF_ERROR;
            } else {
                return ReadState.JSON_RECORD_PARSE_ERROR;
            }
        } else {
            throw ex;
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 88 with JsonToken

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

the class MongoBinaryValueParser method parseV1Format.

// Parse field: { ($binary | $type) ^ ...
private void parseV1Format(String fieldName, TokenIterator tokenizer) {
    boolean sawData = false;
    for (; ; ) {
        // key: ^value
        JsonToken token = requireScalar(tokenizer);
        if (fieldName.equals(ExtendedTypeNames.BINARY)) {
            if (sawData) {
                throw syntaxError();
            }
            sawData = true;
            listener.onValue(token, tokenizer);
        }
        // key: value ^(} | key ...)
        token = tokenizer.requireNext();
        if (token == JsonToken.END_OBJECT) {
            break;
        }
        if (token != JsonToken.FIELD_NAME) {
            throw syntaxError();
        }
        fieldName = tokenizer.textValue();
        switch(fieldName) {
            case ExtendedTypeNames.BINARY:
            case ExtendedTypeNames.BINARY_TYPE:
                break;
            default:
                throw syntaxError();
        }
    }
    if (!sawData) {
        throw syntaxError();
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 89 with JsonToken

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

the class MongoBinaryValueParser method parse.

@Override
public void parse(TokenIterator tokenizer) {
    // Null: assume the value is null
    // (Extension to extended types)
    JsonToken token = tokenizer.requireNext();
    if (token == JsonToken.VALUE_NULL) {
        listener.onValue(token, tokenizer);
        return;
    }
    // This is a harmless extension to the standard.
    if (token.isScalarValue()) {
        listener.onValue(token, tokenizer);
        return;
    }
    // Must be an object
    requireToken(token, JsonToken.START_OBJECT);
    // { ^($binary | $type)
    requireToken(tokenizer, JsonToken.FIELD_NAME);
    String fieldName = tokenizer.textValue();
    if (fieldName.equals(ExtendedTypeNames.BINARY_TYPE)) {
        // { $type ^
        parseV1Format(fieldName, tokenizer);
    } else if (!fieldName.equals(ExtendedTypeNames.BINARY)) {
        throw syntaxError();
    } else if (tokenizer.peek() == JsonToken.START_OBJECT) {
        // { $binary: ^{
        parseV2Format(tokenizer);
    } else {
        // { $binary: ^value
        parseV1Format(fieldName, tokenizer);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 90 with JsonToken

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

the class MongoDateValueParser method parse.

@Override
public void parse(TokenIterator tokenizer) {
    // Null: assume the value is null
    // (Extension to extended types)
    JsonToken token = tokenizer.requireNext();
    if (token == JsonToken.VALUE_NULL) {
        listener.onValue(token, tokenizer);
        return;
    }
    // (Extension to extended types: allow strings.)
    if (token.isScalarValue()) {
        listener.onValue(token, tokenizer);
        return;
    }
    // Must be an object
    requireToken(token, JsonToken.START_OBJECT);
    // Field name must be correct
    requireField(tokenizer, ExtendedTypeNames.DATE);
    // If value is an object, assume V2 canonical format.
    token = tokenizer.requireNext();
    if (token == JsonToken.START_OBJECT) {
        tokenizer.unget(token);
        parseExtended(tokenizer, ExtendedTypeNames.LONG);
    } else {
        // Otherwise, Value must be a scalar
        tokenizer.unget(token);
        listener.onValue(requireScalar(tokenizer), tokenizer);
    }
    // Must be no other fields
    requireToken(tokenizer, JsonToken.END_OBJECT);
}
Also used : 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