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

io.alkal.kalium.kafka.JsonSerializer Maven / Gradle / Ivy

Go to download

An Apache Kafka implementation of KaliumQueueAdapter. current commit_idv0.0.3-3-g3e03de0.dirty

There is a newer version: 0.0.4
Show newest version
package io.alkal.kalium.kafka;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Serializer;

import java.util.Map;

public class JsonSerializer implements Serializer {

    private final ObjectMapper objectMapper;

    /**
     * Default constructor needed by Kafka
     */
    public JsonSerializer() {
        objectMapper = new ObjectMapper();
    }

    //for test purposes
    public JsonSerializer(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

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

    @Override
    public byte[] serialize(String topic, Object data) {
        if (data == null)
            return null;

        try {
            return objectMapper.writeValueAsBytes(data);
        } catch (Exception e) {
            throw new SerializationException("Error serializing JSON message", e);
        }
    }

    @Override
    public void close() {
    }

}