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

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

Go to download

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

There is a newer version: 0.1.2
Show newest version
package com.github.daniel.shuy.kafka.jackson.serializer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Serializer;

/**
 * Serializer for Kafka that uses Jackson to marshall Objects to JSON
 * 
 * @param  Type to be serialized from.
 */
public class KafkaJacksonSerializer implements Serializer {
    private final ObjectMapper mapper;

    /**
     * Returns a new instance of {@link KafkaJacksonSerializer}.
     * 
     * @param mapper 
     *                  The Jackson {@link ObjectMapper}.
     */
    public KafkaJacksonSerializer(ObjectMapper mapper) {
        this.mapper = mapper;
    }

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

    @Override
    public byte[] serialize(String topic, T data) {
        try {
            return mapper.writeValueAsBytes(data);
        } catch (JsonProcessingException | RuntimeException e) {
            throw new SerializationException("Error serializing to JSON with Jackson", e);
        }
    }

    @Override
    public void close() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy