package com.admin.util;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json工具类
*
* @author chengdaolu
* @date 2017年5月28日
*/
public class JsonUtil {
private static final ObjectMapper json = new ObjectMapper()
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
public String toString(Object obj) throws JsonProcessingException {
return obj == null ? "" : json.writeValueAsString(obj);
}
public static <T> T toObj(String str, Class<T> valueType)
throws JsonParseException, JsonMappingException, IOException {
if (StringUtils.isEmpty(str)) {
return null;
}
return json.readValue(str, valueType);
}
public static <T> T toList(String str, TypeReference<T> valueTypeRef)
throws JsonParseException, JsonMappingException, IOException {
if (StringUtils.isEmpty(str) || null == valueTypeRef) {
return null;
}
return json.readValue(str, valueTypeRef);
}
}