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

de.otto.synapse.state.ChronicleMapBytesMarshaller Maven / Gradle / Ivy

Go to download

A library used at otto.de to implement Spring Boot based event-sourcing microservices.

There is a newer version: 0.33.1
Show newest version
package de.otto.synapse.state;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.util.ReadResolvable;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;

import java.io.IOException;

import static de.otto.synapse.translator.ObjectMappers.currentObjectMapper;

public final class ChronicleMapBytesMarshaller implements
        BytesWriter,
        BytesReader,
        ReadResolvable {

    private final ObjectMapper objectMapper;
    private final Class clazz;

    public ChronicleMapBytesMarshaller(Class clazz) {
        this.objectMapper = currentObjectMapper();
        this.clazz = clazz;
    }

    public ChronicleMapBytesMarshaller(ObjectMapper objectMapper,
                                       Class clazz) {
        this.objectMapper = objectMapper;
        this.clazz = clazz;
    }

    @Override
    public V read(Bytes in, V using) {
        try {
            return objectMapper.readValue(in.inputStream(), clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Bytes out, V toWrite) {
        try {
            objectMapper.writeValue(out.writer(), toWrite);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ChronicleMapBytesMarshaller readResolve() {
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy