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 deserializeFromJsonBytes(final byte[] jsonBytes, final Class 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 deserializeEventsMetadata(final byte[] metaData) { final var tr = new TypeReference>() { }; try { return objectMapper.readValue(metaData, tr); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } public static byte[] serializeEventsMetadata(final HashMap metaData) { try { final var valueAsString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(metaData); return valueAsString.getBytes(StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } }