Skip to content

Commit

Permalink
JsonMapper etter anbefaling fra Jackson-sjefskoder
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen committed Dec 3, 2024
1 parent 017c131 commit ba55251
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 <T> List<T> fromJson(String json, TypeReference<List<T>> typeReference) {
try {
return MAPPER.readValue(json, typeReference);
Expand All @@ -60,15 +63,15 @@ public static <T> List<T> fromJson(String json, TypeReference<List<T>> typeRefer

public static <T> T fromJson(String json, Class<T> clazz) {
try {
return MAPPER.readValue(json, clazz);
return MAPPER.readerFor(clazz).readValue(json);
} catch (IOException e) {
throw deserializationException(e);
}
}

public static <T> T fromJson(URL json, Class<T> clazz) {
try {
return MAPPER.readValue(json, clazz);
return MAPPER.readerFor(clazz).readValue(json, clazz);
} catch (IOException e) {
throw deserializationException(e);
}
Expand Down

0 comments on commit ba55251

Please sign in to comment.