package org.example.util;
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.google.gson.Gson;
|
|
import java.io.IOException;
|
|
|
public class JsonUtil {
|
private static final ObjectMapper objectMapper = createObjectMapper();
|
private static Gson gson = new Gson();
|
|
public static String writeValue(Object obj) throws IOException {
|
return objectMapper.writeValueAsString(obj);
|
}
|
|
public static String toJson(Object obj) {
|
return gson.toJson(obj);
|
}
|
|
public static <T> T readValue(String s, TypeReference<T> ref) throws IOException {
|
return objectMapper.readValue(s, ref);
|
}
|
|
|
private static ObjectMapper createObjectMapper() {
|
|
final ObjectMapper mapper = new ObjectMapper();
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
// disabled features:
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
return mapper;
|
}
|
|
}
|