cn.bif.common.JsonUtils Maven / Gradle / Ivy
The newest version!
package cn.bif.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
@Slf4j
public class JsonUtils {
private static ObjectMapper mapper;
/**
* 设置一些通用的属性
*/
static {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 允许key没有双引号
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// 允许key有单引号
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 允许整数以0开头
mapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
// 允许字符串中存在回车换行控制符
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
public static String toJSONString(Object obj) {
return obj != null ? toJSONString(obj, () -> "", false) : "";
}
public static String toFormatJSONString(Object obj) {
return obj != null ? toJSONString(obj, () -> "", true) : "";
}
public static String toJSONString(Object obj, Supplier defaultSupplier, boolean format) {
try {
if (obj == null) {
return defaultSupplier.get();
}
if (obj instanceof String) {
return obj.toString();
}
if (obj instanceof Number) {
return obj.toString();
}
if (format) {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}
return mapper.writeValueAsString(obj);
} catch (Throwable e) {
log.error(String.format("toJSONString %s", obj != null ? obj.toString() : "null"), e);
}
return defaultSupplier.get();
}
public static T toJavaObject(String value, Class tClass) {
return StringUtils.isNotBlank(value) ? toJavaObject(value, tClass, () -> null) : null;
}
public static T toJavaObject(Object obj, Class tClass) {
return obj != null ? toJavaObject(toJSONString(obj), tClass, () -> null) : null;
}
public static T toJavaObject(String value, Class tClass, Supplier defaultSupplier) {
try {
if (StringUtils.isBlank(value)) {
return defaultSupplier.get();
}
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.readValue(value, tClass);
} catch (Throwable e) {
log.error(String.format("toJavaObject exception: \n %s\n %s", value, tClass), e);
}
return defaultSupplier.get();
}
public static List toJavaObjectList(String value, Class tClass) {
return StringUtils.isNotBlank(value) ? toJavaObjectList(value, tClass, () -> null) : null;
}
public static List toJavaObjectList(Object obj, Class tClass) {
return obj != null ? toJavaObjectList(toJSONString(obj), tClass, () -> null) : null;
}
public static List toJavaObjectList(String value, Class tClass, Supplier> defaultSupplier) {
try {
if (StringUtils.isBlank(value)) {
return defaultSupplier.get();
}
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, tClass);
return mapper.readValue(value, javaType);
} catch (Throwable e) {
log.error(String.format("toJavaObjectList exception \n%s\n%s", value, tClass), e);
}
return defaultSupplier.get();
}
// 简单地直接用json复制或者转换(Cloneable)
public static T jsonCopy(Object obj, Class tClass) {
return obj != null ? toJavaObject(toJSONString(obj), tClass) : null;
}
public static Map toMap(String value) {
return StringUtils.isNotBlank(value) ? toMap(value, () -> null) : null;
}
public static Map toMap(Object value) {
return value != null ? toMap(value, () -> null) : null;
}
public static Map toMap(Object value, Supplier