From ba5525174b656941ccbbd48c7cbd7fa75cc7c267 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen Date: Tue, 3 Dec 2024 22:52:59 +0100 Subject: [PATCH] JsonMapper etter anbefaling fra Jackson-sjefskoder --- .../vedtak/mapper/json/DefaultJsonMapper.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/felles/mapper/src/main/java/no/nav/vedtak/mapper/json/DefaultJsonMapper.java b/felles/mapper/src/main/java/no/nav/vedtak/mapper/json/DefaultJsonMapper.java index 0f2dc68d9..be5f65840 100644 --- a/felles/mapper/src/main/java/no/nav/vedtak/mapper/json/DefaultJsonMapper.java +++ b/felles/mapper/src/main/java/no/nav/vedtak/mapper/json/DefaultJsonMapper.java @@ -6,13 +6,16 @@ import java.util.Map; import java.util.TimeZone; +import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -24,32 +27,32 @@ private DefaultJsonMapper() { } - private static final ObjectMapper MAPPER = createObjectMapper(); + private static final JsonMapper MAPPER = createObjectMapper(); public static ObjectMapper getObjectMapper() { return MAPPER; } - private static ObjectMapper createObjectMapper() { - return new ObjectMapper().registerModule(new Jdk8Module()) - .registerModule(new JavaTimeModule()) - .setTimeZone(TimeZone.getTimeZone("Europe/Oslo")) - .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) + private static JsonMapper createObjectMapper() { + return JsonMapper.builder() + .addModule(new Jdk8Module()) + .addModule(new JavaTimeModule()) + .defaultTimeZone(TimeZone.getTimeZone("Europe/Oslo")) + .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) // Trengs denne? Sak har kjørt uten .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) - .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) + .serializationInclusion(JsonInclude.Include.NON_EMPTY) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) - .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE); + .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE) + .visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE) + .visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE) + .visibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE) + .visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) + .visibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY) + .build(); } - // TODO: Vurder om disse bør med - tas ifm rydding i integrasjon-dtos (mest mulig records) - // .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); - // .setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE); - // .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE); - // .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); - // .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY); - public static List fromJson(String json, TypeReference> typeReference) { try { return MAPPER.readValue(json, typeReference); @@ -60,7 +63,7 @@ public static List fromJson(String json, TypeReference> typeRefer public static T fromJson(String json, Class clazz) { try { - return MAPPER.readValue(json, clazz); + return MAPPER.readerFor(clazz).readValue(json); } catch (IOException e) { throw deserializationException(e); } @@ -68,7 +71,7 @@ public static T fromJson(String json, Class clazz) { public static T fromJson(URL json, Class clazz) { try { - return MAPPER.readValue(json, clazz); + return MAPPER.readerFor(clazz).readValue(json, clazz); } catch (IOException e) { throw deserializationException(e); }