Search in sources :

Example 6 with JsonToken

use of org.codehaus.jackson.JsonToken in project st-js by st-js.

the class JSArrayDeserializer method handleNonArray.

/**
 * Helper method called when current token is no START_ARRAY. Will either throw an exception, or try to handle value
 * as if member of implicit array, depending on configuration.
 */
private final Array<Object> handleNonArray(JsonParser jp, DeserializationContext ctxt, Array<Object> result) throws IOException, JsonProcessingException {
    // [JACKSON-526]: implicit arrays from single values?
    if (!ctxt.isEnabled(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
        throw ctxt.mappingException(_collectionType.getRawClass());
    }
    JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    JsonToken t = jp.getCurrentToken();
    Object value;
    if (t == JsonToken.VALUE_NULL) {
        value = null;
    } else if (typeDeser == null) {
        value = valueDes.deserialize(jp, ctxt);
    } else {
        value = valueDes.deserializeWithType(jp, ctxt, typeDeser);
    }
    result.push(value);
    return result;
}
Also used : JsonToken(org.codehaus.jackson.JsonToken) TypeDeserializer(org.codehaus.jackson.map.TypeDeserializer)

Example 7 with JsonToken

use of org.codehaus.jackson.JsonToken in project st-js by st-js.

the class JSMapDeserializer method deserialize.

/*
	 * /********************************************************** /* JsonDeserializer API
	 * /**********************************************************
	 */
/**
 * {@inheritDoc}
 */
@Override
public Map<String, Object> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // Ok: must point to START_OBJECT, FIELD_NAME or END_OBJECT
    JsonToken t = jp.getCurrentToken();
    if ((t != JsonToken.START_OBJECT) && (t != JsonToken.FIELD_NAME) && (t != JsonToken.END_OBJECT)) {
        throw ctxt.mappingException(getMapClass());
    }
    Map<String, Object> result = JSCollections.$map();
    _readAndBind(jp, ctxt, result);
    return result;
}
Also used : JsonToken(org.codehaus.jackson.JsonToken)

Example 8 with JsonToken

use of org.codehaus.jackson.JsonToken in project st-js by st-js.

the class JSMapDeserializer method deserialize.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, Object> deserialize(JsonParser jp, DeserializationContext ctxt, Map<String, Object> result) throws IOException, JsonProcessingException {
    // Ok: must point to START_OBJECT or FIELD_NAME
    JsonToken t = jp.getCurrentToken();
    if ((t != JsonToken.START_OBJECT) && (t != JsonToken.FIELD_NAME)) {
        throw ctxt.mappingException(getMapClass());
    }
    _readAndBind(jp, ctxt, result);
    return result;
}
Also used : JsonToken(org.codehaus.jackson.JsonToken)

Example 9 with JsonToken

use of org.codehaus.jackson.JsonToken in project st-js by st-js.

the class JSMapDeserializer method _readAndBind.

/*
	 * /********************************************************** /* Internal methods
	 * /**********************************************************
	 */
/**
 * <p>_readAndBind.</p>
 *
 * @param jp a {@link org.codehaus.jackson.JsonParser} object.
 * @param ctxt a {@link org.codehaus.jackson.map.DeserializationContext} object.
 * @param result a {@link org.stjs.javascript.Map} object.
 * @throws java.io.IOException if any.
 * @throws org.codehaus.jackson.JsonProcessingException if any.
 */
protected final void _readAndBind(JsonParser jp, DeserializationContext ctxt, Map<String, Object> result) throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.START_OBJECT) {
        t = jp.nextToken();
    }
    final KeyDeserializer keyDes = _keyDeserializer;
    final JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        // Must point to field name
        String fieldName = jp.getCurrentName();
        Object key = (keyDes == null) ? fieldName : keyDes.deserializeKey(fieldName, ctxt);
        // And then the value...
        t = jp.nextToken();
        // Note: must handle null explicitly here; value deserializers won't
        Object value;
        if (t == JsonToken.VALUE_NULL) {
            value = null;
        } else if (typeDeser == null) {
            value = valueDes.deserialize(jp, ctxt);
        } else {
            value = valueDes.deserializeWithType(jp, ctxt, typeDeser);
        }
        /*
			 * !!! 23-Dec-2008, tatu: should there be an option to verify that there are no duplicate field names?
			 * (and/or what to do, keep-first or keep-last)
			 */
        result.$put(key.toString(), value);
    }
}
Also used : JsonToken(org.codehaus.jackson.JsonToken) TypeDeserializer(org.codehaus.jackson.map.TypeDeserializer) KeyDeserializer(org.codehaus.jackson.map.KeyDeserializer)

Example 10 with JsonToken

use of org.codehaus.jackson.JsonToken in project presto-hive-apache by prestodb.

the class JsonSerDe method extractCurrentField.

/**
 * Utility method to extract current expected field from given JsonParser
 *
 * isTokenCurrent is a boolean variable also passed in, which determines
 * if the JsonParser is already at the token we expect to read next, or
 * needs advancing to the next before we read.
 */
private Object extractCurrentField(JsonParser p, HCatFieldSchema hcatFieldSchema, boolean isTokenCurrent) throws IOException {
    Object val = null;
    JsonToken valueToken;
    if (isTokenCurrent) {
        valueToken = p.getCurrentToken();
    } else {
        valueToken = p.nextToken();
    }
    switch(hcatFieldSchema.getType()) {
        case INT:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getIntValue();
            break;
        case TINYINT:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getByteValue();
            break;
        case SMALLINT:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getShortValue();
            break;
        case BIGINT:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getLongValue();
            break;
        case BOOLEAN:
            String bval = (valueToken == JsonToken.VALUE_NULL) ? null : p.getText();
            if (bval != null) {
                val = Boolean.valueOf(bval);
            } else {
                val = null;
            }
            break;
        case FLOAT:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getFloatValue();
            break;
        case DOUBLE:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getDoubleValue();
            break;
        case STRING:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : p.getText();
            break;
        case BINARY:
            throw new IOException("JsonSerDe does not support BINARY type");
        case DATE:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : Date.valueOf(p.getText());
            break;
        case TIMESTAMP:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : tsParser.parseTimestamp(p.getText());
            break;
        case DECIMAL:
            val = (valueToken == JsonToken.VALUE_NULL) ? null : HiveDecimal.create(p.getText());
            break;
        case VARCHAR:
            int vLen = ((BaseCharTypeInfo) hcatFieldSchema.getTypeInfo()).getLength();
            val = (valueToken == JsonToken.VALUE_NULL) ? null : new HiveVarchar(p.getText(), vLen);
            break;
        case CHAR:
            int cLen = ((BaseCharTypeInfo) hcatFieldSchema.getTypeInfo()).getLength();
            val = (valueToken == JsonToken.VALUE_NULL) ? null : new HiveChar(p.getText(), cLen);
            break;
        case ARRAY:
            if (valueToken == JsonToken.VALUE_NULL) {
                val = null;
                break;
            }
            if (valueToken != JsonToken.START_ARRAY) {
                throw new IOException("Start of Array expected");
            }
            List<Object> arr = new ArrayList<Object>();
            while ((valueToken = p.nextToken()) != JsonToken.END_ARRAY) {
                arr.add(extractCurrentField(p, hcatFieldSchema.getArrayElementSchema().get(0), true));
            }
            val = arr;
            break;
        case MAP:
            if (valueToken == JsonToken.VALUE_NULL) {
                val = null;
                break;
            }
            if (valueToken != JsonToken.START_OBJECT) {
                throw new IOException("Start of Object expected");
            }
            Map<Object, Object> map = new LinkedHashMap<Object, Object>();
            HCatFieldSchema valueSchema = hcatFieldSchema.getMapValueSchema().get(0);
            while ((valueToken = p.nextToken()) != JsonToken.END_OBJECT) {
                Object k = getObjectOfCorrespondingPrimitiveType(p.getCurrentName(), hcatFieldSchema.getMapKeyTypeInfo());
                Object v = extractCurrentField(p, valueSchema, false);
                map.put(k, v);
            }
            val = map;
            break;
        case STRUCT:
            if (valueToken == JsonToken.VALUE_NULL) {
                val = null;
                break;
            }
            if (valueToken != JsonToken.START_OBJECT) {
                throw new IOException("Start of Object expected");
            }
            HCatSchema subSchema = hcatFieldSchema.getStructSubSchema();
            int sz = subSchema.getFieldNames().size();
            List<Object> struct = new ArrayList<Object>(Collections.nCopies(sz, null));
            while ((valueToken = p.nextToken()) != JsonToken.END_OBJECT) {
                populateRecord(struct, valueToken, p, subSchema);
            }
            val = struct;
            break;
        default:
            LOG.error("Unknown type found: " + hcatFieldSchema.getType());
            return null;
    }
    return val;
}
Also used : BaseCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) LinkedHashMap(java.util.LinkedHashMap) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) JsonToken(org.codehaus.jackson.JsonToken)

Aggregations

JsonToken (org.codehaus.jackson.JsonToken)19 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 JsonParseException (org.codehaus.jackson.JsonParseException)3 JsonParser (org.codehaus.jackson.JsonParser)3 TypeDeserializer (org.codehaus.jackson.map.TypeDeserializer)3 LinkedHashMap (java.util.LinkedHashMap)2 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)2 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)2 BaseCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo)2 HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)2 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)2 Neo4jError (org.neo4j.server.rest.transactional.error.Neo4jError)2 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)2 EvalError (com.google.refine.expr.EvalError)1 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)1 GwtTestJSONException (com.googlecode.gwt.test.exceptions.GwtTestJSONException)1 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1