Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mutualise ObjectMapper configuration #7

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions scribe-core/src/main/java/fr/maif/json/EventEnvelopeJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.maif.eventsourcing.Event;
import fr.maif.eventsourcing.EventEnvelope;
import fr.maif.eventsourcing.format.JacksonEventFormat;
Expand All @@ -22,16 +19,7 @@

public class EventEnvelopeJson {

private final static ObjectMapper mapper = buildMapper();

private static ObjectMapper buildMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper;
}
private final static ObjectMapper mapper = MapperSingleton.getInstance();

public static <E extends Event, Meta, Context> String serializeToString(EventEnvelope<E, Meta, Context> event,
JacksonEventFormat<?, E> format,
Expand Down
26 changes: 26 additions & 0 deletions scribe-core/src/main/java/fr/maif/json/MapperSingleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.maif.json;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public final class MapperSingleton {
public final static ObjectMapper mapper = buildMapper();

// Prevent instantiation
private MapperSingleton() { }

public static ObjectMapper getInstance() {
return mapper;
}

private static ObjectMapper buildMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fr.maif.jooq.PgAsyncTransaction;
import fr.maif.jooq.QueryResult;
import fr.maif.json.Json;
import fr.maif.json.MapperSingleton;
import io.vavr.API;
import io.vavr.Tuple;
import io.vavr.Tuple0;
Expand Down Expand Up @@ -85,7 +86,7 @@ public ReactivePostgresEventStore(
this.eventFormat = eventFormat;
this.metaFormat = metaFormat;
this.contextFormat = contextFormat;
this.objectMapper = new ObjectMapper();
this.objectMapper = MapperSingleton.getInstance();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import fr.maif.json.MapperSingleton;
import io.vavr.Tuple0;
import fr.maif.eventsourcing.Event;
import fr.maif.eventsourcing.EventEnvelope;
Expand Down Expand Up @@ -106,7 +107,7 @@ public PostgresEventStore(ActorSystem system, EventPublisher<E, Meta, Context> e
this.eventFormat = eventFormat;
this.metaFormat = metaFormat;
this.contextFormat = contextFormat;
this.objectMapper = new ObjectMapper();
this.objectMapper = MapperSingleton.getInstance();
}

public static <E extends Event, Meta, Context> PostgresEventStore<E, Meta, Context> create(
Expand Down