diff --git a/borestop/src/main/resources/config/application.yml b/borestop/src/main/resources/config/application.yml index aac8814b92c7641cfdb3d3f47e9d6aab2341b2fc..c6f99b1b771a056e0547bc661ed80c1924f5fd1a 100644 --- a/borestop/src/main/resources/config/application.yml +++ b/borestop/src/main/resources/config/application.yml @@ -60,6 +60,12 @@ management: spring: application: name: borestop + jackson: + default-property-inclusion: non_empty + serialization: + write-dates-as-timestamps: false + deserialization: + fail-on-unknown-properties: false profiles: # The commented value for `active` can be replaced with valid Spring profiles to load. # Otherwise, it will be filled in by maven when building the JAR file diff --git a/borestop/src/test/java/com/ippon/borestop/infrastructure/primay/TestJson.java b/borestop/src/test/java/com/ippon/borestop/infrastructure/primay/TestJson.java new file mode 100644 index 0000000000000000000000000000000000000000..99e86c5a38eca3631de44551cc74aedeeaa331b9 --- /dev/null +++ b/borestop/src/test/java/com/ippon/borestop/infrastructure/primay/TestJson.java @@ -0,0 +1,42 @@ +package com.ippon.borestop.infrastructure.primay; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import java.io.IOException; + +public final class TestJson { + private static final ObjectMapper jsonMapper = jsonMapper(); + + private TestJson() {} + + public static ObjectMapper jsonMapper() { + return new ObjectMapper() + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) + .registerModule(new JavaTimeModule()) + .registerModule(new Jdk8Module()) + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + } + + public static <T> String writeAsString(T object) { + try { + return jsonMapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + throw new AssertionError("Error serializing object: " + e.getMessage(), e); + } + } + + public static <T> T readFromJson(String json, Class<T> clazz) { + try { + return jsonMapper.readValue(json, clazz); + } catch (IOException e) { + throw new AssertionError("Error reading value from json: " + e.getMessage(), e); + } + } +}