io.quarkus.kafka.client.serialization.ObjectMapperDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kafka-client Show documentation
Show all versions of quarkus-kafka-client Show documentation
Connect to Apache Kafka with its native API
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