use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType("varchar(x)") Slice value) {
String valueString = value.toStringUtf8();
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if (token == VALUE_STRING && valueString.equals(parser.getValueAsString())) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class JsonOperators method castToBoolean.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Boolean result;
switch(parser.getCurrentToken()) {
case VALUE_NULL:
result = null;
break;
case VALUE_STRING:
result = VarcharOperators.castToBoolean(Slices.utf8Slice(parser.getText()));
break;
case VALUE_NUMBER_FLOAT:
result = DoubleOperators.castToBoolean(parser.getDoubleValue());
break;
case VALUE_NUMBER_INT:
result = BigintOperators.castToBoolean(parser.getLongValue());
break;
case VALUE_TRUE:
result = true;
break;
case VALUE_FALSE:
result = false;
break;
default:
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN));
}
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
return result;
} catch (IOException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN));
}
}
use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class JsonOperators method castToInteger.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(INTEGER)
public static Long castToInteger(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result;
switch(parser.getCurrentToken()) {
case VALUE_NULL:
result = null;
break;
case VALUE_STRING:
result = VarcharOperators.castToInteger(Slices.utf8Slice(parser.getText()));
break;
case VALUE_NUMBER_FLOAT:
result = DoubleOperators.castToInteger(parser.getDoubleValue());
break;
case VALUE_NUMBER_INT:
result = (long) toIntExact(parser.getLongValue());
break;
case VALUE_TRUE:
result = BooleanOperators.castToInteger(true);
break;
case VALUE_FALSE:
result = BooleanOperators.castToInteger(false);
break;
default:
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER));
}
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER");
return result;
} catch (ArithmeticException | IOException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER));
}
}
use of com.fasterxml.jackson.core.JsonParser in project presto by prestodb.
the class TestJsonExtract method doExtract.
private static String doExtract(JsonExtractor<Slice> jsonExtractor, String json) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(json);
// Advance to the first token
jsonParser.nextToken();
Slice extract = jsonExtractor.extract(jsonParser);
return (extract == null) ? null : extract.toStringUtf8();
}
use of com.fasterxml.jackson.core.JsonParser in project spring-security-oauth by spring-projects.
the class JwkSetConverter method convert.
/**
* Converts the supplied <code>InputStream</code> to a <code>Set</code> of {@link JwkDefinition}(s).
*
* @param jwkSetSource the source for the JWK Set
* @return a <code>Set</code> of {@link JwkDefinition}(s)
* @throws JwkException if the JWK Set JSON object is invalid
*/
@Override
public Set<JwkDefinition> convert(InputStream jwkSetSource) {
Set<JwkDefinition> jwkDefinitions;
JsonParser parser = null;
try {
parser = this.factory.createParser(jwkSetSource);
if (parser.nextToken() != JsonToken.START_OBJECT) {
throw new JwkException("Invalid JWK Set Object.");
}
if (parser.nextToken() != JsonToken.FIELD_NAME) {
throw new JwkException("Invalid JWK Set Object.");
}
if (!parser.getCurrentName().equals(KEYS)) {
throw new JwkException("Invalid JWK Set Object. The JWK Set MUST have a " + KEYS + " attribute.");
}
if (parser.nextToken() != JsonToken.START_ARRAY) {
throw new JwkException("Invalid JWK Set Object. The JWK Set MUST have an array of JWK(s).");
}
jwkDefinitions = new LinkedHashSet<JwkDefinition>();
Map<String, String> attributes = new HashMap<String, String>();
while (parser.nextToken() == JsonToken.START_OBJECT) {
while (parser.nextToken() == JsonToken.FIELD_NAME) {
String attributeName = parser.getCurrentName();
parser.nextToken();
String attributeValue = parser.getValueAsString();
attributes.put(attributeName, attributeValue);
}
JwkDefinition jwkDefinition = this.createJwkDefinition(attributes);
if (!jwkDefinitions.add(jwkDefinition)) {
throw new JwkException("Duplicate JWK found in Set: " + jwkDefinition.getKeyId() + " (" + KEY_ID + ")");
}
attributes.clear();
}
} catch (IOException ex) {
throw new JwkException("An I/O error occurred while reading the JWK Set: " + ex.getMessage(), ex);
} finally {
try {
if (parser != null)
parser.close();
} catch (IOException ex) {
}
}
return jwkDefinitions;
}
Aggregations