Search in sources :

Example 16 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class RequiredDistributionJsonDeserializer method deserialize.

@Override
public RequiredDistribution deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
    DistributionType type = DistributionType.valueOf(jsonNode.get("type").asText().toUpperCase());
    switch(type) {
        case ANY:
            return InputProperty.ANY_DISTRIBUTION;
        case SINGLETON:
            return InputProperty.SINGLETON_DISTRIBUTION;
        case BROADCAST:
            return InputProperty.BROADCAST_DISTRIBUTION;
        case UNKNOWN:
            return InputProperty.UNKNOWN_DISTRIBUTION;
        case HASH:
            JsonNode keysNode = jsonNode.get("keys");
            if (keysNode == null) {
                throw new TableException("Hash distribution requires non-empty hash keys.");
            }
            int[] keys = new int[keysNode.size()];
            for (int i = 0; i < keysNode.size(); ++i) {
                keys[i] = keysNode.get(i).asInt();
            }
            return InputProperty.hashDistribution(keys);
        default:
            throw new TableException("Unsupported distribution type: " + type);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) DistributionType(org.apache.flink.table.planner.plan.nodes.exec.InputProperty.DistributionType)

Example 17 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class ResolvedExpressionJsonDeserializer method deserialize.

@Override
public ResolvedExpression deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    ObjectNode jsonNode = jsonParser.readValueAsTree();
    String expressionType = Optional.ofNullable(jsonNode.get(TYPE)).map(JsonNode::asText).orElse(TYPE_REX_NODE_EXPRESSION);
    if (TYPE_REX_NODE_EXPRESSION.equals(expressionType)) {
        return deserializeRexNodeExpression(jsonNode, jsonParser.getCodec(), ctx);
    } else {
        throw new ValidationException(String.format("Expression '%s' cannot be deserialized. " + "Currently, only SQL expressions can be deserialized from the persisted plan.", jsonNode));
    }
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)

Example 18 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project druid by druid-io.

the class GroupByQueryQueryToolChest method decorateObjectMapper.

@Override
public ObjectMapper decorateObjectMapper(final ObjectMapper objectMapper, final GroupByQuery query) {
    final boolean resultAsArray = query.getContextBoolean(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false);
    // Serializer that writes array- or map-based rows as appropriate, based on the "resultAsArray" setting.
    final JsonSerializer<ResultRow> serializer = new JsonSerializer<ResultRow>() {

        @Override
        public void serialize(final ResultRow resultRow, final JsonGenerator jg, final SerializerProvider serializers) throws IOException {
            if (resultAsArray) {
                jg.writeObject(resultRow.getArray());
            } else {
                jg.writeObject(resultRow.toMapBasedRow(query));
            }
        }
    };
    // Deserializer that can deserialize either array- or map-based rows.
    final JsonDeserializer<ResultRow> deserializer = new JsonDeserializer<ResultRow>() {

        @Override
        public ResultRow deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
            if (jp.isExpectedStartObjectToken()) {
                final Row row = jp.readValueAs(Row.class);
                return ResultRow.fromLegacyRow(row, query);
            } else {
                return ResultRow.of(jp.readValueAs(Object[].class));
            }
        }
    };
    class GroupByResultRowModule extends SimpleModule {

        private GroupByResultRowModule() {
            addSerializer(ResultRow.class, serializer);
            addDeserializer(ResultRow.class, deserializer);
        }
    }
    final ObjectMapper newObjectMapper = objectMapper.copy();
    newObjectMapper.registerModule(new GroupByResultRowModule());
    return newObjectMapper;
}
Also used : JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Row(org.apache.druid.data.input.Row) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 19 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project java-chassis by ServiceComb.

the class ArgWrapperJavaType method readValue.

public Map<String, Object> readValue(ObjectMapper mapper, String json) throws IOException {
    Map<String, Object> args = new LinkedHashMap<>();
    JsonParser jp = mapper.getFactory().createParser(json);
    DeserializationContext deserializationContext = ObjectMapperUtils.createDeserializationContext(mapper, jp);
    jp.nextToken();
    for (String fieldName = jp.nextFieldName(); fieldName != null; fieldName = jp.nextFieldName()) {
        jp.nextToken();
        ArgInfo argInfo = argInfos.get(fieldName);
        if (argInfo == null) {
            continue;
        }
        if (argInfo.deserializer == null) {
            argInfo.deserializer = deserializationContext.findRootValueDeserializer(argInfo.javaType);
        }
        args.put(fieldName, argInfo.deserializer.deserialize(jp, deserializationContext));
    }
    return args;
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) LinkedHashMap(java.util.LinkedHashMap) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 20 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project sts4 by spring-projects.

the class JSON method pathAsJson.

private static SimpleModule pathAsJson() {
    SimpleModule m = new SimpleModule();
    m.addSerializer(Path.class, new JsonSerializer<Path>() {

        @Override
        public void serialize(Path path, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            gen.writeString(path.toString());
        }
    });
    m.addDeserializer(Path.class, new JsonDeserializer<Path>() {

        @Override
        public Path deserialize(JsonParser parse, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            return Paths.get(parse.getText());
        }
    });
    return m;
}
Also used : Path(java.nio.file.Path) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)28 JsonParser (com.fasterxml.jackson.core.JsonParser)21 IOException (java.io.IOException)17 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)7 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)7 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)6 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)6 Map (java.util.Map)6 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)5 List (java.util.List)5 ObjectNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Iterator (java.util.Iterator)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3