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

io.codemonastery.dropwizard.kinesis.EventObjectMapper Maven / Gradle / Ivy

The newest version!
package io.codemonastery.dropwizard.kinesis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;

import javax.annotation.Nullable;
import java.nio.ByteBuffer;

public final class EventObjectMapper implements EventEncoder, EventDecoder {

    private final ObjectMapper objectMapper;
    private final Class klass;

    public EventObjectMapper(ObjectMapper objectMapper, @Nullable Class klass) {
        Preconditions.checkNotNull(objectMapper);
        this.objectMapper = objectMapper;
        this.klass = klass;
    }

    @Nullable
    @Override
    public E decode(ByteBuffer bytes) throws Exception {
        if(klass == null){
            throw new UnsupportedOperationException("cannot decode if event class was not specified");
        }
        return objectMapper.readValue(bytes.array(), klass);
    }

    @Nullable
    @Override
    public byte[] encode(E event) throws Exception {
        return objectMapper.writeValueAsBytes(event);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy