Search in sources :

Example 66 with JsonToken

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

the class DruidConnectionImpl method parseFieldForName.

private void parseFieldForName(List<String> fieldNames, List<ColumnMetaData.Rep> fieldTypes, int posTimestampField, Row.RowBuilder rowBuilder, JsonParser parser, String fieldName) throws IOException {
    // Move to next token, which is name's value
    JsonToken token = parser.nextToken();
    boolean isTimestampColumn = fieldName.equals(DEFAULT_RESPONSE_TIMESTAMP_COLUMN);
    int i = fieldNames.indexOf(fieldName);
    ColumnMetaData.Rep type = null;
    if (i < 0) {
        if (!isTimestampColumn) {
            // Field not present
            return;
        }
    } else {
        type = fieldTypes.get(i);
    }
    if (isTimestampColumn || ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP == type) {
        final int fieldPos = posTimestampField != -1 ? posTimestampField : i;
        if (token == JsonToken.VALUE_NUMBER_INT) {
            rowBuilder.set(posTimestampField, parser.getLongValue());
            return;
        } else {
            // Thus need to guess via try and catch
            synchronized (UTC_TIMESTAMP_FORMAT) {
                // synchronized block to avoid race condition
                try {
                    // First try to parse as Timestamp with timezone.
                    rowBuilder.set(fieldPos, UTC_TIMESTAMP_FORMAT.parse(parser.getText()).getTime());
                } catch (ParseException e) {
                    // swallow the exception and try timestamp format
                    try {
                        rowBuilder.set(fieldPos, TIMESTAMP_FORMAT.parse(parser.getText()).getTime());
                    } catch (ParseException e2) {
                        // unknown format should not happen
                        throw new RuntimeException(e2);
                    }
                }
            }
            return;
        }
    }
    switch(token) {
        case VALUE_NUMBER_INT:
            if (type == null) {
                type = ColumnMetaData.Rep.LONG;
            }
        // fall through
        case VALUE_NUMBER_FLOAT:
            if (type == null) {
                // JSON's "float" is 64-bit floating point
                type = ColumnMetaData.Rep.DOUBLE;
            }
            switch(type) {
                case BYTE:
                    rowBuilder.set(i, parser.getByteValue());
                    break;
                case SHORT:
                    rowBuilder.set(i, parser.getShortValue());
                    break;
                case INTEGER:
                    rowBuilder.set(i, parser.getIntValue());
                    break;
                case LONG:
                    rowBuilder.set(i, parser.getLongValue());
                    break;
                case DOUBLE:
                    rowBuilder.set(i, parser.getDoubleValue());
                    break;
            }
            break;
        case VALUE_TRUE:
            rowBuilder.set(i, true);
            break;
        case VALUE_FALSE:
            rowBuilder.set(i, false);
            break;
        case VALUE_NULL:
            break;
        case VALUE_STRING:
        default:
            final String s = parser.getText();
            if (type != null) {
                switch(type) {
                    case LONG:
                    case PRIMITIVE_LONG:
                    case SHORT:
                    case PRIMITIVE_SHORT:
                    case INTEGER:
                    case PRIMITIVE_INT:
                        switch(s) {
                            case "Infinity":
                            case "-Infinity":
                            case "NaN":
                                throw new RuntimeException("/ by zero");
                        }
                        rowBuilder.set(i, Long.valueOf(s));
                        break;
                    case FLOAT:
                    case PRIMITIVE_FLOAT:
                    case PRIMITIVE_DOUBLE:
                    case NUMBER:
                    case DOUBLE:
                        switch(s) {
                            case "Infinity":
                                rowBuilder.set(i, Double.POSITIVE_INFINITY);
                                return;
                            case "-Infinity":
                                rowBuilder.set(i, Double.NEGATIVE_INFINITY);
                                return;
                            case "NaN":
                                rowBuilder.set(i, Double.NaN);
                                return;
                        }
                        rowBuilder.set(i, Double.valueOf(s));
                        break;
                }
            } else {
                rowBuilder.set(i, s);
            }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) ParseException(java.text.ParseException) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 67 with JsonToken

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

the class BsonParser method isExpectedStartArrayToken.

@Override
public boolean isExpectedStartArrayToken() {
    JsonToken t = _currToken;
    if (t == JsonToken.START_OBJECT) {
        // FIX FOR ISSUE #31:
        // if this method is called, this usually means the caller wants
        // to parse an array (i.e. this method is called by array
        // deserializers such as StringArrayDeserializer. If we're currently
        // at the start of an object, check if this object might as well
        // be an array (it's just a quick sanity check).
        boolean isarray;
        if (_in.markSupported()) {
            _in.mark(3);
            try {
                // check the first key in the object. if it is '0' this
                // could indeed be an array
                // read type
                byte tpe = _in.readByte();
                if (tpe != BsonConstants.TYPE_END) {
                    // read key (CString)
                    if (_in.readByte() == '0' && _in.readByte() == '\0') {
                        // the object could indeed be an array!
                        isarray = true;
                    } else {
                        // the first key was not '0'. this can't be an array!
                        isarray = false;
                    }
                } else {
                    // object is empty. it could be an empty array.
                    isarray = true;
                }
            } catch (IOException e) {
                // we cannot check. just assume it would work. the caller
                // should know what he does.
                isarray = true;
            } finally {
                try {
                    _in.reset();
                } catch (IOException re) {
                    throw new IllegalStateException("Could not reset input stream", re);
                }
            }
        } else {
            // we cannot check. just assume it would work. the caller
            // should know what he does.
            isarray = true;
        }
        if (isarray) {
            // replace START_OBJECT token by START_ARRAY, update current context
            _currToken = JsonToken.START_ARRAY;
            _currentContext = _currentContext.copy(_currentContext.parent, true);
            return true;
        }
    }
    return super.isExpectedStartArrayToken();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Example 68 with JsonToken

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

the class JacksonStreamingTestMain method getCurrentWeatherData.

public static void getCurrentWeatherData(final CurrentWeatherData currentWeatherData, final JsonParser jParser) throws JsonParseException, IOException {
    if (jParser.nextToken() == JsonToken.START_OBJECT) {
        while (jParser.nextToken() != JsonToken.END_OBJECT) {
            final String fieldname = jParser.getCurrentName();
            final JsonToken nextToken = jParser.nextToken();
            if (nextToken == JsonToken.START_OBJECT) {
                getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
            }
            if (nextToken == JsonToken.START_ARRAY) {
                JsonToken tokenNext = jParser.nextToken();
                while (tokenNext != JsonToken.END_ARRAY) {
                    if (tokenNext == JsonToken.START_OBJECT) {
                        getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
                    }
                    tokenNext = jParser.nextToken();
                }
            }
            if ((nextToken == JsonToken.VALUE_NUMBER_INT) || (nextToken == JsonToken.VALUE_STRING)) {
                getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname);
            }
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 69 with JsonToken

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

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 70 with JsonToken

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

the class VectorOutput method innerRun.

protected boolean innerRun() throws IOException {
    JsonToken t = parser.nextToken();
    if (t != JsonToken.FIELD_NAME) {
        return false;
    }
    String possibleTypeName = parser.getText();
    if (!possibleTypeName.isEmpty() && possibleTypeName.charAt(0) == '$') {
        switch(possibleTypeName) {
            case ExtendedTypeName.BINARY:
                writeBinary(checkNextToken(JsonToken.VALUE_STRING));
                checkCurrentToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.TYPE:
                if (checkNextToken(JsonToken.VALUE_NUMBER_INT) || !hasBinary()) {
                    throw UserException.parseError().message("Either $type is not an integer or has no $binary").build(LOG);
                }
                writeBinary(checkNextToken(JsonToken.VALUE_STRING));
                checkCurrentToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.DATE:
                writeDate(checkNextToken(JsonToken.VALUE_STRING));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.TIME:
                writeTime(checkNextToken(JsonToken.VALUE_STRING));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.TIMESTAMP:
                writeTimestamp(checkNextToken(JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.INTERVAL:
                writeInterval(checkNextToken(JsonToken.VALUE_STRING));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.INTEGER:
                writeInteger(checkNextToken(JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
            case ExtendedTypeName.DECIMAL:
                writeDecimal(checkNextToken(JsonToken.VALUE_NUMBER_FLOAT, JsonToken.VALUE_NUMBER_INT));
                checkNextToken(JsonToken.END_OBJECT);
                return true;
        }
    }
    return false;
}
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