com.github.meixuesong.aggregatepersistence.JsonComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aggregate-persistence Show documentation
Show all versions of aggregate-persistence Show documentation
Aggregate persistence library
package com.github.meixuesong.aggregatepersistence;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class JsonComparator implements PropertyComparator {
private ObjectMapper mapper;
@Override
public boolean isAllPropertiesEqual(T a, T b) {
String jsonA = getJson(a);
String jsonB = getJson(b);
return jsonB.equals(jsonA);
}
private String getJson(T object) {
try {
return getMapper().writeValueAsString(object);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private synchronized ObjectMapper getMapper() {
if (mapper == null) {
createObjectMapper();
}
return mapper;
}
private void createObjectMapper() {
if (this.mapper == null) {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.mapper = mapper;
}
}
}