com.tosan.tools.util.ToStringJsonUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tosan-tostring-builder Show documentation
Show all versions of tosan-tostring-builder Show documentation
Library provides interface for use in toString method and determine each field how should be
shown. also, you can choose from two implementations provided for simple and json formatting.
also, provides interface for use in aspect to log service call.
The newest version!
package com.tosan.tools.util;
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;
/**
* @author Mostafa Abdollahi
* @since 6/10/2021
*/
public class ToStringJsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.enable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static ObjectMapper getMapper() {
return mapper;
}
public static String toJson(T object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
return ToStringConstant.ERROR_JSON + e.getMessage();
}
}
}