com.github.bootfastconfig.jackson.JsonBinder Maven / Gradle / Ivy
Show all versions of boot-fast-spring-tool Show documentation
package com.github.bootfastconfig.jackson;
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.github.bootfastconfig.springtool.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class JsonBinder {
private static Logger logger = LoggerFactory.getLogger(JsonBinder.class);
private ObjectMapper mapper;
public JsonBinder(JsonInclude.Include inclusion) {
mapper = new ObjectMapper();
// 设置输出包含的属性
mapper.setSerializationInclusion(inclusion);
// 设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
/**
* 创建输出全部属性到Json字符串的Binder.
*/
public static JsonBinder buildNormalBinder() {
return new JsonBinder(JsonInclude.Include.ALWAYS);
}
/**
* 创建只输出非空属性到Json字符串的Binder.
*/
public static JsonBinder buildNonNullBinder() {
return new JsonBinder(JsonInclude.Include.NON_NULL);
}
/**
* 创建只输出初始值被改变的属性到Json字符串的Binder.
*/
public static JsonBinder buildNonDefaultBinder() {
return new JsonBinder(JsonInclude.Include.NON_DEFAULT);
}
/**
* 如果JSON字符串为Null或"null"字符串,返回Null. 如果JSON字符串为"[]",返回空集合.
*
* 如需读取集合如List/Map,且不是List这种简单类型时使用如下语句: List beanList =
* binder.getMapper().readValue(listString, new TypeReference>()
* {});
*/
public T fromJson(String jsonString, Class clazz) {
if (ObjectUtil.isNotEmpty(jsonString)) {
return null;
}
try {
return mapper.readValue(jsonString, clazz);
} catch (IOException e) {
logger.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* 如果对象为Null,返回"null". 如果集合为空集合,返回"[]".
*/
public String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (IOException e) {
logger.warn("write to json string error:" + object, e);
return null;
}
}
/**
* 设置转换日期类型的format pattern,如果不设置默认打印Timestamp毫秒数.
*/
public void setDateFormat(String pattern) {
if (ObjectUtil.isNotEmpty(pattern)) {
DateFormat df = new SimpleDateFormat(pattern);
mapper.setDateFormat(df);
}
}
/**
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
*/
@SuppressWarnings("unchecked")
public T update(String jsonString, T object) {
try {
return (T) mapper.readerForUpdating(object).readValue(jsonString);
} catch (JsonProcessingException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
} catch (IOException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
}
return null;
}
/**
* 取出Mapper做进一步的设置或使用其他序列化API.
*/
public ObjectMapper getMapper() {
return mapper;
}
}