use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class JsonOperators method castToDouble.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(DOUBLE)
public static Double castToDouble(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Double result;
switch(parser.getCurrentToken()) {
case VALUE_NULL:
result = null;
break;
case VALUE_STRING:
result = VarcharOperators.castToDouble(Slices.utf8Slice(parser.getText()));
break;
case VALUE_NUMBER_FLOAT:
result = parser.getDoubleValue();
break;
case VALUE_NUMBER_INT:
// An alternative is calling getLongValue and then BigintOperators.castToDouble.
// It doesn't work as well because it can result in overflow and underflow exceptions for large integral numbers.
result = parser.getDoubleValue();
break;
case VALUE_TRUE:
result = BooleanOperators.castToDouble(true);
break;
case VALUE_FALSE:
result = BooleanOperators.castToDouble(false);
break;
default:
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE));
}
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DOUBLE");
return result;
} catch (IOException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE));
}
}
use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class TestTupleDomain method testJsonSerialization.
@Test
public void testJsonSerialization() throws Exception {
TestingTypeManager typeManager = new TestingTypeManager();
TestingBlockEncodingSerde blockEncodingSerde = new TestingBlockEncodingSerde(typeManager);
ObjectMapper mapper = new ObjectMapperProvider().get().registerModule(new SimpleModule().addDeserializer(ColumnHandle.class, new JsonDeserializer<ColumnHandle>() {
@Override
public ColumnHandle deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return new ObjectMapperProvider().get().readValue(jsonParser, TestingColumnHandle.class);
}
}).addDeserializer(Type.class, new TestingTypeDeserializer(typeManager)).addSerializer(Block.class, new TestingBlockJsonSerde.Serializer(blockEncodingSerde)).addDeserializer(Block.class, new TestingBlockJsonSerde.Deserializer(blockEncodingSerde)));
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.all();
assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
}));
tupleDomain = TupleDomain.none();
assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
}));
tupleDomain = TupleDomain.fromFixedValues(ImmutableMap.of(A, NullableValue.of(BIGINT, 1L), B, NullableValue.asNull(VARCHAR)));
assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
}));
}
use of com.fasterxml.jackson.core.JsonParser in project json-android-compare by martinadamek.
the class JacksonJson method parsePublicTimeline.
public List<Map> parsePublicTimeline(InputStream inputStream) {
List<Map> result = new ArrayList<Map>();
try {
Map map;
String key;
String key2;
JsonParser p = sJsonFactory.createJsonParser(inputStream);
p.nextToken();
while (p.nextToken() != JsonToken.END_ARRAY) {
map = new HashMap();
while (p.nextToken() != JsonToken.END_OBJECT) {
key = p.getCurrentName();
// move to value, or START_OBJECT/START_ARRAY
p.nextToken();
if (p.getCurrentToken() == JsonToken.START_OBJECT) {
while (p.nextToken() != JsonToken.END_OBJECT) {
key2 = p.getCurrentName();
// move to value, or START_OBJECT/START_ARRAY
p.nextToken();
map.put("user." + key2, p.getText());
}
} else {
map.put(key, p.getText());
}
}
result.add(map);
}
p.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
use of com.fasterxml.jackson.core.JsonParser in project bagheera by mozilla-metrics.
the class Validator method isValidJson.
public boolean isValidJson(String json) {
boolean isValid = false;
JsonParser parser = null;
try {
parser = jsonFactory.createJsonParser(json);
while (parser.nextToken() != null) {
// noop
}
isValid = true;
} catch (JsonParseException ex) {
LOG.error("JSON parse error");
} catch (IOException e) {
LOG.error("JSON IO error");
} finally {
if (parser != null) {
try {
parser.close();
} catch (IOException e) {
LOG.error("Error closing JSON parser", e);
}
}
}
return isValid;
}
use of com.fasterxml.jackson.core.JsonParser in project bagheera by mozilla-metrics.
the class JsonValidator method isValid.
@Override
public boolean isValid(byte[] data) {
boolean isValid = false;
JsonParser parser = null;
try {
parser = jsonFactory.createJsonParser(data);
while (parser.nextToken() != null) {
// noop
}
isValid = true;
} catch (JsonParseException ex) {
LOG.error("JSON parse error");
} catch (IOException e) {
LOG.error("JSON IO error");
} catch (Exception e) {
LOG.error("Generic error during validation: ", e);
} finally {
if (parser != null) {
try {
parser.close();
} catch (IOException e) {
LOG.error("Error closing JSON parser", e);
}
}
}
return isValid;
}
Aggregations