Skip to content

Commit

Permalink
Make Jackson ObjectMapper initialization thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Sep 23, 2024
1 parent 9b0326b commit 516610b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@

public class Json31 {

private static ObjectMapper mapper;
private static ObjectMapper converterMapper;
private static final class ObjectMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJson31();
}

private static final class ConverterMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJsonConverter();
}

static Logger LOGGER = LoggerFactory.getLogger(Json31.class);

public static ObjectMapper mapper() {
if (mapper == null) {
mapper = ObjectMapperFactory.createJson31();
}
return mapper;
return ObjectMapperHolder.MAPPER;
}

public static ObjectMapper converterMapper() {
if (converterMapper == null) {
converterMapper = ObjectMapperFactory.createJsonConverter();
}
return converterMapper;
return ConverterMapperHolder.MAPPER;
}

public static ObjectWriter pretty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.fasterxml.jackson.databind.ObjectWriter;

public class Yaml {
static ObjectMapper mapper;

private static final class ObjectMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml();
}

public static ObjectMapper mapper() {
if (mapper == null) {
mapper = ObjectMapperFactory.createYaml();
}
return mapper;
return ObjectMapperHolder.MAPPER;
}

public static ObjectWriter pretty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
import java.util.Map;

public class Yaml31 {
static ObjectMapper mapper;

private static final class ObjectMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml31();
}


static Logger LOGGER = LoggerFactory.getLogger(Yaml31.class);

public static ObjectMapper mapper() {
if (mapper == null) {
mapper = ObjectMapperFactory.createYaml31();
}
return mapper;
return ObjectMapperHolder.MAPPER;
}

public static ObjectWriter pretty() {
Expand Down

0 comments on commit 516610b

Please sign in to comment.