JacksonUtil Jdk8Module added along with jackson-datatype-jdk8 dependency to address the Optional mapping

This commit is contained in:
Sergey Matvienko 2024-04-23 11:00:35 +02:00
parent ce24f2e765
commit 1f1086f106
2 changed files with 15 additions and 2 deletions

View File

@ -104,6 +104,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
@ -54,22 +55,30 @@ import java.util.regex.Pattern;
@Slf4j
public class JacksonUtil {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder()
.addModule(new Jdk8Module())
.build();
public static final ObjectMapper PRETTY_SORTED_JSON_MAPPER = JsonMapper.builder()
.addModule(new Jdk8Module())
.enable(SerializationFeature.INDENT_OUTPUT)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.build();
public static ObjectMapper ALLOW_UNQUOTED_FIELD_NAMES_MAPPER = JsonMapper.builder()
.addModule(new Jdk8Module())
.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.build();
public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = JsonMapper.builder()
.addModule(new Jdk8Module())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();
public static ObjectMapper getObjectMapperWithJavaTimeModule() {
return new ObjectMapper().registerModule(new JavaTimeModule());
return JsonMapper.builder()
.addModule(new Jdk8Module())
.addModule(new JavaTimeModule())
.build();
}
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {