Search in sources :

Example 21 with JsonParser

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;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 22 with JsonParser

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));
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 23 with JsonParser

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));
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 24 with JsonParser

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();
}
Also used : Slice(io.airlift.slice.Slice) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 25 with JsonParser

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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)137 IOException (java.io.IOException)37 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)24 StringWriter (java.io.StringWriter)17 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 JsonToken (com.fasterxml.jackson.core.JsonToken)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5