de.otto.synapse.state.ChronicleMapBytesMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of synapse-core Show documentation
Show all versions of synapse-core Show documentation
A library used at otto.de to implement Spring Boot based event-sourcing microservices.
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;
}
}