Skip to content

Commit

Permalink
refactor: isolated final mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Oct 17, 2023
1 parent c8a29bf commit ab89acd
Showing 1 changed file with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
public class BusinessEntityConverter {

private static final Logger LOGGER = Logger.getLogger(BusinessEntityConverter.class);
public static ObjectMapper objectMapper;
private static final ObjectMapper MAPPER = objectMapper();

private static ObjectMapper objectMapper() {
return JsonMapper.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.build();
}

public BusinessEntityConverter() {}

public static String entityToJsonValue(Object entity) {
try {
return getObjectMapper().writeValueAsString(entity);
return MAPPER.writeValueAsString(entity);
} catch (JsonProcessingException e) {
LOGGER.error("Can't write json value", e);
throw new ExpressImportException(e);
Expand All @@ -33,7 +40,7 @@ public static String entityToJsonValue(Object entity) {

public static <T> T jsonValueToEntity(String jsonValue, Class<T> classType) {
try {
return getObjectMapper().readValue(jsonValue, classType);
return MAPPER.readValue(jsonValue, classType);
} catch (IOException e) {
LOGGER.error("Can't read json value", e);
throw new ExpressImportException(e);
Expand All @@ -45,25 +52,16 @@ public static <T> List<T> jsonValueToEntities(String jsonValue, Class<T> classTy
return new ArrayList<>();
}
try {
CollectionType type = getObjectMapper().getTypeFactory().constructCollectionType(List.class, classType);
return getObjectMapper().readValue(jsonValue, type);
CollectionType type = MAPPER.getTypeFactory().constructCollectionType(List.class, classType);
return MAPPER.readValue(jsonValue, type);
} catch (IOException e) {
LOGGER.error("Can't read json value", e);
throw new ExpressImportException(e);
}
}

public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
return getObjectMapper().convertValue(fromValue, toValueType);
return MAPPER.convertValue(fromValue, toValueType);
}

public static ObjectMapper getObjectMapper() {
if (objectMapper == null) {
objectMapper = JsonMapper.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.build();
}
return objectMapper;
}
}

0 comments on commit ab89acd

Please sign in to comment.