All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.kafka.client.serialization.ObjectMapperDeserializer Maven / Gradle / Ivy

There is a newer version: 3.18.0.CR1
Show newest version
package io.quarkus.kafka.client.serialization;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class ObjectMapperDeserializer implements Deserializer {

    private final JavaType type;
    private final ObjectMapper objectMapper;

    public ObjectMapperDeserializer(Class type) {
        this(type, ObjectMapperProducer.get());
    }

    public ObjectMapperDeserializer(Class type, ObjectMapper objectMapper) {
        this.type = TypeFactory.defaultInstance().constructType(type);
        this.objectMapper = objectMapper;
    }

    public ObjectMapperDeserializer(TypeReference typeReference) {
        this(typeReference, ObjectMapperProducer.get());
    }

    public ObjectMapperDeserializer(TypeReference typeReference, ObjectMapper objectMapper) {
        this.type = TypeFactory.defaultInstance().constructType(typeReference);
        this.objectMapper = objectMapper;
    }

    @Override
    public void configure(Map configs, boolean isKey) {
    }

    @Override
    public T deserialize(String topic, byte[] data) {
        if (data == null) {
            return null;
        }

        try (InputStream is = new ByteArrayInputStream(data)) {
            return objectMapper.readValue(is, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void close() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy