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

com.github.daniel.shuy.kafka.jackson.serializer.KafkaJacksonDeserializer Maven / Gradle / Ivy

Go to download

Serializer/Deserializer for Kafka that uses Jackson to marshall/unmarshall Objects to/from JSON

The newest version!
package com.github.daniel.shuy.kafka.jackson.serializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Deserializer;

/**
 * Deserializer for Kafka that uses Jackson to unmarshall Objects from JSON
 *
 * @param  Type to be deserialized into.
 */
public class KafkaJacksonDeserializer implements Deserializer {
    private final ObjectMapper mapper;
    private final Class clazz;

    /**
     * Returns a new instance of {@link KafkaJacksonDeserializer}.
     * 
     * @param mapper 
     *                  The Jackson {@link ObjectMapper}.
     * @param clazz 
     *                  Class to be deserialized into.
     */
    public KafkaJacksonDeserializer(ObjectMapper mapper, Class clazz) {
        this.mapper = mapper;
        this.clazz = clazz;
    }

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

    @Override
    public T deserialize(String topic, byte[] data) {
        try {
            return mapper.readValue(data, clazz);
        } catch (IOException | RuntimeException e) {
            throw new SerializationException("Error deserializing from JSON with Jackson", e);
        }
    }

    @Override
    public void close() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy