feature: add event sourcing microservice init
This commit is contained in:
68
src/main/java/com/eventsourcing/es/SerializerUtils.java
Normal file
68
src/main/java/com/eventsourcing/es/SerializerUtils.java
Normal file
@@ -0,0 +1,68 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user