JsonUtils.java

  1. package no.nav.data.common.utils;

  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.DeserializationFeature;
  5. import com.fasterxml.jackson.databind.JsonNode;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.SerializationFeature;
  8. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  9. import no.nav.data.common.exceptions.TechnicalException;
  10. import org.springframework.core.ParameterizedTypeReference;

  11. import java.io.IOException;
  12. import java.util.List;
  13. import java.util.Map;

  14. public final class JsonUtils {

  15.     public static final ParameterizedTypeReference<List<String>> STRING_LIST = new ParameterizedTypeReference<>() {
  16.     };
  17.     public static final ParameterizedTypeReference<List<String>> INT_LIST = new ParameterizedTypeReference<>() {
  18.     };

  19.     private JsonUtils() {
  20.     }

  21.     private static final ObjectMapper objectMapper = createObjectMapper();
  22.     private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<>() {
  23.     };

  24.     public static ObjectMapper createObjectMapper() {
  25.         var om = new ObjectMapper();
  26.         om.registerModule(new JavaTimeModule());
  27.         om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  28.         om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  29.         return om;
  30.     }

  31.     public static ObjectMapper getObjectMapper() {
  32.         return objectMapper;
  33.     }

  34.     public static JsonNode toJsonNode(String json) {
  35.         try {
  36.             return objectMapper.readTree(json);
  37.         } catch (JsonProcessingException e) {
  38.             throw new IllegalArgumentException("invalid json ", e);
  39.         }
  40.     }

  41.     public static Map toMap(Object object) {
  42.         return objectMapper.convertValue(object, MAP_TYPE_REFERENCE);
  43.     }

  44.     public static <T> T toObject(String jsonString, TypeReference<T> reference) {
  45.         try {
  46.             return objectMapper.readValue(jsonString, reference);
  47.         } catch (Exception e) {
  48.             throw new IllegalArgumentException("invalid json ", e);
  49.         }
  50.     }

  51.     public static <T> T toObject(String jsonPayload, Class<T> type) {
  52.         try {
  53.             return objectMapper.readValue(jsonPayload, type);
  54.         } catch (IOException e) {
  55.             throw new IllegalArgumentException("invalid json ", e);
  56.         }
  57.     }

  58.     public static String toJson(Object object) {
  59.         try {
  60.             return objectMapper.writeValueAsString(object);
  61.         } catch (JsonProcessingException e) {
  62.             throw new IllegalArgumentException("cannot convert to json", e);
  63.         }
  64.     }

  65.     public static <T> T readValue(String jsonString, TypeReference<T> type) {
  66.         try {
  67.             return objectMapper.readValue(jsonString, type);
  68.         } catch (Exception e) {
  69.             throw new TechnicalException("json error", e);
  70.         }
  71.     }

  72.     public static <T> T toObject(JsonNode jsonNode, Class<T> clazz) {
  73.         try {
  74.             return objectMapper.treeToValue(jsonNode, clazz);
  75.         } catch (JsonProcessingException e) {
  76.             throw new TechnicalException("cannot create object from json", e);
  77.         }
  78.     }

  79.     public static JsonNode toJsonNode(Object object) {
  80.         return objectMapper.valueToTree(object);
  81.     }

  82.     @SuppressWarnings("unchecked")
  83.     public static <T> T cloneObject(T object) {
  84.         return (T) toObject(toJsonNode(object), object.getClass());
  85.     }

  86. }