Search in sources :

Example 91 with JsonToken

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

the class ExtendedTypeFieldFactory method buildExtendedTypeParser.

private ElementParser buildExtendedTypeParser(FieldDefn fieldDefn) {
    // Extended types are objects: { "$type": ... }
    // Extended arrays are [ { "$type": ...
    TokenIterator tokenizer = fieldDefn.tokenizer();
    JsonToken token = tokenizer.requireNext();
    ElementParser parser;
    switch(token) {
        case START_OBJECT:
            parser = extendedTypeParserFor(fieldDefn, false);
            break;
        case START_ARRAY:
            parser = arrayParserFor(fieldDefn);
            break;
        default:
            parser = null;
    }
    tokenizer.unget(token);
    return parser;
}
Also used : TokenIterator(org.apache.drill.exec.store.easy.json.parser.TokenIterator) JsonToken(com.fasterxml.jackson.core.JsonToken) ElementParser(org.apache.drill.exec.store.easy.json.parser.ElementParser)

Example 92 with JsonToken

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

the class NullValueParser method parse.

/**
 * Parses nulls. On the first non-null
 * Parses <code>true | false | null | integer | float | string|
 *              embedded-object | { ... } | [ ... ]</code>
 */
@Override
public void parse(TokenIterator tokenizer) {
    JsonToken token = tokenizer.requireNext();
    if (token != JsonToken.VALUE_NULL) {
        tokenizer.unget(token);
        resolve(tokenizer).parse(tokenizer);
    // This parser never called again
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 93 with JsonToken

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

the class JsonStructureParser method recover.

/**
 * Attempt recovery from a JSON syntax error by skipping to the next
 * record. The Jackson parser is quite limited in its recovery abilities.
 *
 * @return {@code true}  if another record can be read, {@code false}
 * if EOF.
 * @throws org.apache.drill.common.exceptions.UserException if the error is unrecoverable
 * @see <a href="https://issues.apache.org/jira/browse/DRILL-4653">DRILL-4653</a>
 * @see <a href="https://issues.apache.org/jira/browse/DRILL-5953">DRILL-5953</a>
 */
private boolean recover() {
    logger.warn("Attempting recovery from JSON syntax error. " + tokenizer.context());
    boolean firstAttempt = true;
    while (true) {
        while (true) {
            try {
                if (tokenizer.getParser().isClosed()) {
                    throw errorFactory().unrecoverableError();
                }
                JsonToken token = tokenizer.next();
                if (token == null) {
                    if (firstAttempt && !options().skipMalformedDocument) {
                        throw errorFactory().unrecoverableError();
                    }
                    return false;
                }
                if (token == JsonToken.NOT_AVAILABLE) {
                    return false;
                }
                if (token == JsonToken.END_OBJECT) {
                    break;
                }
                firstAttempt = false;
            } catch (RecoverableJsonException e) {
            // Ignore, keep trying
            }
        }
        try {
            JsonToken token = tokenizer.next();
            if (token == null || token == JsonToken.NOT_AVAILABLE) {
                return false;
            }
            if (token == JsonToken.START_OBJECT) {
                logger.warn("Attempting to resume JSON parse. " + tokenizer.context());
                tokenizer.unget(token);
                errorRecoveryCount++;
                return true;
            }
        } catch (RecoverableJsonException e) {
        // Ignore, keep trying
        }
    }
}
Also used : RecoverableJsonException(org.apache.drill.exec.store.easy.json.parser.TokenIterator.RecoverableJsonException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 94 with JsonToken

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

the class JCardRawReader method checkCurrent.

private void checkCurrent(JsonToken expected) throws JCardParseException {
    JsonToken actual = parser.getCurrentToken();
    check(expected, actual);
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 95 with JsonToken

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

the class ProxyResource method rewriteResponse.

private static byte[] rewriteResponse(byte[] input, Function<String, String> uriRewriter) {
    try {
        JsonParser parser = JSON_FACTORY.createParser(input);
        ByteArrayOutputStream out = new ByteArrayOutputStream(input.length * 2);
        JsonGenerator generator = JSON_FACTORY.createGenerator(out);
        JsonToken token = parser.nextToken();
        if (token != START_OBJECT) {
            throw invalidJson("bad start token: " + token);
        }
        generator.copyCurrentEvent(parser);
        while (true) {
            token = parser.nextToken();
            if (token == null) {
                throw invalidJson("unexpected end of stream");
            }
            if (token == END_OBJECT) {
                generator.copyCurrentEvent(parser);
                break;
            }
            if (token == FIELD_NAME) {
                String name = parser.getValueAsString();
                if (!"nextUri".equals(name) && !"partialCancelUri".equals(name)) {
                    generator.copyCurrentStructure(parser);
                    continue;
                }
                token = parser.nextToken();
                if (token != VALUE_STRING) {
                    throw invalidJson(format("bad %s token: %s", name, token));
                }
                String value = parser.getValueAsString();
                value = uriRewriter.apply(value);
                generator.writeStringField(name, value);
                continue;
            }
            throw invalidJson("unexpected token: " + token);
        }
        token = parser.nextToken();
        if (token != null) {
            throw invalidJson("unexpected token after object close: " + token);
        }
        generator.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new ProxyException(e);
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonToken(com.fasterxml.jackson.core.JsonToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

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