Skip to content
Snippets Groups Projects
Commit 49176d36 authored by Colin DAMON's avatar Colin DAMON
Browse files

Json configuration and tooling

parent 393f1f76
No related branches found
No related tags found
1 merge request!22Resolve "Borestop"
......@@ -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
......
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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment