cn.luues.tool.json.JsonUtils Maven / Gradle / Ivy
The newest version!
package cn.luues.tool.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
// 日起格式化
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
static {
//对象的所有字段全部列入
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
//取消默认转换timestamps形式
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
//排序
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false);
/*objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);*/
//忽略空Bean转json的错误
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
//所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
//忽略 在json字符串中存在,但是在java对象中不存在对应属性的情况。防止错误
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
// 注册一个时间序列化及反序列化的处理模块,用于解决jdk8中localDateTime等的序列化问题
objectMapper.registerModule(new JavaTimeModule());
}
public static ObjectMapper getObjectMapper(){
return objectMapper;
}
public static void registerModule(Module module){
objectMapper.registerModule(module);
}
/**
* 创建一个JSONObject对象
* @return JsonNode
*/
public static ObjectNode createObjectNode(){
return objectMapper.createObjectNode();
}
/** 对象与Json字符串之间的转换 */
/**
* 对象转Json格式字符串
* @param obj 对象
* @return Json格式字符串
*/
@SneakyThrows
public static String toJson(T obj) {
if (obj == null) {
return null;
}
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
}
/**
* 对象转Json格式字符串(格式化的Json字符串)
* @param obj 对象
* @return 美化的Json格式字符串
*/
@SneakyThrows
public static String toJsonPretty(T obj) {
if (obj == null) {
return null;
}
return obj instanceof String ? (String) obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}
/**
* 字符串转换为自定义对象
* @param json 要转换的字符串
* @param clazz 自定义对象的class对象
* @return 自定义对象
*/
@SneakyThrows
public static T parse(String json, Class clazz) {
if(StringUtils.isEmpty(json) || clazz == null){
return null;
}
return clazz.equals(String.class) ? (T) json : objectMapper.readValue(json, clazz);
}
public static T parseUrlParams(String paramStr, Class clazz){
String[] params = paramStr.split("&");
ObjectNode objectNode = JsonUtils.createObjectNode();
for (int i = 0; i < params.length; i++) {
String[] param = params[i].split("=");
if (param.length >= 2) {
String key = param[0];
String value = param[1];
for (int j = 2; j < param.length; j++) {
value += "=" + param[j];
}
objectNode.put(key,value);
}
}
return parse(JsonUtils.toJson(objectNode), clazz);
}
/**
* 字符串转换为自定义对象
* @param clazz 自定义对象的class对象
* @return 自定义对象
*/
@SneakyThrows
public static T parse(InputStream inputStream, Class clazz) {
if(null == inputStream || clazz == null){
return null;
}
return objectMapper.readValue(inputStream, clazz);
}
@SneakyThrows
public static T convertValue(Object object, Class clazz) {
if(null == object || clazz == null){
return null;
}
return objectMapper.convertValue(object, clazz);
}
/** 集合对象与Json字符串之间的转换 */
@SneakyThrows
public static T parse(String json, TypeReference typeReference) {
if (StringUtils.isEmpty(json) || typeReference == null) {
return null;
}
return (T) (typeReference.getType().equals(String.class) ? json : objectMapper.readValue(json, typeReference));
}
@SneakyThrows
public static T parse(String str, Class> collectionClazz, Class>... elementClazzes) {
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
return objectMapper.readValue(str, javaType);
}
public static ArrayNode valueToTree(List> list){
return objectMapper.valueToTree(list);
}
/**
* json => 对象处理方法
*
* 参数clazz和type必须一个为null,另一个不为null
*
* 此方法不对外暴露,访问权限为private
*
* @param json 源json串
* @param clazz 对象类
* @param type 对象类型
* @param 泛型
*/
@SneakyThrows
private static T parse(String json, Class clazz, TypeReference type) {
T obj = null;
if (StringUtils.isNotEmpty(json)) {
if (clazz != null) {
obj = objectMapper.readValue(json, clazz);
} else {
obj = (T) objectMapper.readValue(json, type);
}
}
return obj;
}
}