68 lines
2.4 KiB
Java
68 lines
2.4 KiB
Java
package com.eventsourcing.es;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.HashMap;
|
|
|
|
public final class SerializerUtils {
|
|
|
|
private static final ObjectMapper objectMapper = JsonMapper.builder()
|
|
.addModule(new ParameterNamesModule())
|
|
.addModule(new Jdk8Module())
|
|
.addModule(new JavaTimeModule())
|
|
.build();
|
|
|
|
private SerializerUtils() {
|
|
}
|
|
|
|
public static byte[] serializeToJsonBytes(final Object object) {
|
|
try {
|
|
return objectMapper.writeValueAsBytes(object);
|
|
} catch (JsonProcessingException e) {
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static <T> T deserializeFromJsonBytes(final byte[] jsonBytes, final Class<T> valueType) {
|
|
try {
|
|
return objectMapper.readValue(jsonBytes, valueType);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static Event[] deserializeEventsFromJsonBytes(final byte[] jsonBytes) {
|
|
try {
|
|
return objectMapper.readValue(jsonBytes, Event[].class);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static HashMap<String, byte[]> deserializeEventsMetadata(final byte[] metaData) {
|
|
final var tr = new TypeReference<HashMap<String, byte[]>>() {
|
|
};
|
|
try {
|
|
return objectMapper.readValue(metaData, tr);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static byte[] serializeEventsMetadata(final HashMap<String, byte[]> metaData) {
|
|
try {
|
|
final var valueAsString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(metaData);
|
|
return valueAsString.getBytes(StandardCharsets.UTF_8);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
}
|
|
}
|
|
} |