Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFP-5437 JsonMapper etter anbefaling fra Jackson-sjefskoder #1398

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,67 @@
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;

import no.nav.vedtak.exception.TekniskException;

/**
* OBS: JsonMapper er en subklasse av ObjectMapper - tilpasset Json og med en enklere builder
* Se også jackson-core, pakke json, JsonReadFeature og JsonWriteFeature for mer Java/Json-nær konfig/features
*/
public class DefaultJsonMapper {

private DefaultJsonMapper() {

}

private static final ObjectMapper MAPPER = createObjectMapper();
private static final JsonMapper MAPPER = createJsonMapper();

public static ObjectMapper getObjectMapper() {
@Deprecated // Bruk getJsonMapper() når mapper-konfig brukes as is og ikke endres ((de)serializers eller subtypes
public static JsonMapper 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)
// Foretrekker denne - men bruk heller metoder nedenfor direkte enn å assigne til lokale variable
public static JsonMapper getJsonMapper() {
return MAPPER;
}

// Bruk denne for kun for ContextResolver (JacksonJsonConfig-klasser) som skal legge til (de)serializers eller registrere subtypes
public static JsonMapper getCopyFromDefaultJsonMapper() {
return MAPPER.copy();
}

private static JsonMapper createJsonMapper() {
return JsonMapper.builder()
.addModule(new Jdk8Module())
.addModule(new JavaTimeModule())
.defaultTimeZone(TimeZone.getTimeZone("Europe/Oslo"))
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.setSerializationInclusion(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)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) // TODO: Trengs denne? Sak har kjørt lenge uten
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
.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 +77,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
Loading