JsonUtils工具类
1、JSON字符串转换为对象
2、对象转换为JSON字符串
3、对象转换为JSON字符串(不忽略null)
4、对象之间转换
5、List 转化为 List<Map<String, Object>>
6、object 转化为 map
package com.meiyuan.rights.sdk.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.meiyuan.rights.sdk.domain.vo.TokenParam;
import com.meiyuan.rights.sdk.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
@Slf4j
public class JsonUtils {
public static <T> T jsonToObject(@NotNull String json, Class<T> targetType) {
T result;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JavaTimeModule());
result = objectMapper.readValue(json, targetType);
} catch (IOException e) {
log.error("JSON反序列化异常,str={}", json, e);
throw new CustomException("数据格式错误");
}
return result;
}
public static String objectToJson(Object obj) {
String result;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModule(new JavaTimeModule());
result = objectMapper.writeValueAsString(obj);
} catch (IOException e) {
log.error("JSON序列化异常,obj={}", obj, e);
throw new CustomException("数据格式转换失败");
}
return result;
}
public static String objectToJsonNull(Object obj) {
String result;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
objectMapper.registerModule(new JavaTimeModule());
result = objectMapper.writeValueAsString(obj);
} catch (IOException e) {
log.error("JSON序列化异常,obj={}", obj, e);
throw new CustomException("数据格式转换失败");
}
return result;
}
public static String objectToJsonWithNull(Object obj) {
String result;
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
objectMapper.registerModule(new JavaTimeModule());
result = objectMapper.writeValueAsString(obj);
} catch (IOException e) {
log.error("JSON序列化异常,obj={}", obj, e);
throw new CustomException("数据格式转换失败");
}
return result;
}
public static <T> T objectToObject(@NotNull Object source, Class<T> targetType) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(source, targetType);
}
public static List<Map<String, Object>> listObjectToListMap(@NotNull List<?> sourceList) {
ObjectMapper mapper = new ObjectMapper();
CollectionType mapCollectionType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
return mapper.convertValue(sourceList, mapCollectionType);
}
public static Map<String, Object> objectToMap(Object o) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Map map = null;
try {
String json = objectMapper.writeValueAsString(o);
map = objectMapper.readValue(json, Map.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return map;
}
public static JsonNode parseNode(String text) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
return objectMapper.readTree(text);
} catch (IOException e) {
log.warn("parse json error:" + text, e);
return null;
}
}
public static void main(String[] args) {
TokenParam param = new TokenParam();
param.setAppId("123");
System.out.println(objectToMap(param));
}
}