use of org.codehaus.jackson.JsonToken in project hive by apache.
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:
String b = (valueToken == JsonToken.VALUE_NULL) ? null : p.getText();
if (b != null) {
try {
String t = Text.decode(b.getBytes(), 0, b.getBytes().length);
return t.getBytes();
} catch (CharacterCodingException e) {
LOG.warn("Error generating json binary type from object.", e);
return null;
}
} else {
val = null;
}
break;
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;
}
use of org.codehaus.jackson.JsonToken in project st-js by st-js.
the class JSArrayDeserializer method deserialize.
/**
* {@inheritDoc}
*/
@Override
public Array<Object> deserialize(JsonParser jp, DeserializationContext ctxt, Array<Object> result) throws IOException, JsonProcessingException {
// Ok: must point to START_ARRAY (or equivalent)
if (!jp.isExpectedStartArrayToken()) {
return handleNonArray(jp, ctxt, result);
}
JsonDeserializer<Object> valueDes = _valueDeserializer;
JsonToken t;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
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;
}
use of org.codehaus.jackson.JsonToken in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method readMapInMap.
@Override
protected <K, V> Map<K, V> readMapInMap(JsonParser input, Function<JsonParser, K> keyDeserializer, Function<JsonParser, V> valueDeserializer, Map<K, V> map) throws Exception {
JsonToken token = input.getCurrentToken();
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
token = input.nextToken();
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
}
JsonToken currentToken = input.nextToken();
while (currentToken != JsonToken.END_ARRAY) {
if (currentToken != JsonToken.START_OBJECT) {
throw new ValueSerializationException("Expected an object start at " + input.getCurrentLocation().toString());
}
currentToken = input.nextToken();
K key = null;
V value = null;
while (currentToken != JsonToken.END_OBJECT) {
String objectKey = input.getCurrentName();
input.nextToken();
if ("key".equals(objectKey)) {
key = keyDeserializer.map(input);
} else if ("value".equals(objectKey)) {
value = valueDeserializer.map(input);
} else {
// input.nextToken();
input.skipChildren();
}
currentToken = input.nextToken();
}
if (key != null) {
map.put(key, value);
}
currentToken = input.nextToken();
}
return map;
}
use of org.codehaus.jackson.JsonToken in project qi4j-sdk by Qi4j.
the class JacksonValueDeserializer method readArrayInCollection.
@Override
protected <T> Collection<T> readArrayInCollection(JsonParser input, Function<JsonParser, T> deserializer, Collection<T> collection) throws Exception {
JsonToken token = input.getCurrentToken();
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
token = input.nextToken();
}
if (token == JsonToken.VALUE_NULL) {
return null;
}
if (token != JsonToken.START_ARRAY) {
throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
}
while (input.nextToken() != JsonToken.END_ARRAY) {
T element = deserializer.map(input);
collection.add(element);
}
return collection;
}
Aggregations