package com.yuanchu.limslaboratory.utils;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import java.text.SimpleDateFormat;
|
|
/**
|
* @Author 张宾
|
*/
|
public class JsonUtil {
|
|
private static ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
static {
|
JSON_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
JSON_MAPPER.registerModule(new JavaTimeModule());
|
}
|
|
/**
|
* json转换对应实体
|
* @param json
|
* @param clazz
|
* @param <T>
|
* @return
|
*/
|
public static <T> T jsonToPojo(String json, Class<T> clazz){
|
T t=null;
|
try {
|
t=JSON_MAPPER.readValue(json,clazz);
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
return t;
|
}
|
|
/**
|
* 实体转换成json字符
|
* @param t
|
* @param <T>
|
* @return
|
*/
|
public static <T> String jsonToString(T t){
|
String result=null;
|
try {
|
result=JSON_MAPPER.writeValueAsString(t);
|
} catch (JsonProcessingException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
}
|