org.yes.tools.utils.JacksonUtil Maven / Gradle / Ivy
package org.yes.tools.utils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yes.tools.jackson.ObjectMapperDateFormatExtend;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
public class JacksonUtil {
private final static ObjectMapper mapper = new ObjectMapper();
private static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);
static {
DateFormat dateFormat = mapper.getDateFormat();
mapper.setConfig(mapper.getDeserializationConfig().with(new ObjectMapperDateFormatExtend(dateFormat))); //反序列化扩展日期格式支持
}
/**
* json得到对象
*
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static T parse(String jsonString, Class pojoClass) {
T pojo = null;
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
pojo = mapper.readValue(jsonString, pojoClass);
} catch (JsonProcessingException e) {
log.error(e.getMessage());
}
return pojo;
}
/**
* 集合内对象转换
*
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static List toList(List> source, Class tClass) throws IOException {
return toList(toJson(source), tClass);
}
/**
* json得到对象集合
*
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static List toList(String json, Class cls) throws JsonParseException, JsonMappingException, IOException {
ArrayList mList = new ArrayList();
List array = mapper.readValue(json, List.class);
for (int i = 0; i < array.size(); i++) {
if (array.get(i).getClass().equals(cls)) {
mList.add((T) array.get(i));
} else {
mList.add(JacksonUtil.convert(array.get(i), cls));
}
}
return mList;
}
/**
* 获取请求体中的Json
*/
public static byte[] readBytes(InputStream is, int contentLen) throws IOException {
if (contentLen > 0) {
int readLen = 0;
int readLengthThisTime = 0;
byte[] message = new byte[contentLen];
while (readLen != contentLen) {
readLengthThisTime = is.read(message, readLen, contentLen - readLen);
if (readLengthThisTime == -1) {
break;
}
readLen += readLengthThisTime;
}
return message;
}
return new byte[]{};
}
/**
* obj to json
*/
public static String toJson(Object src) {
try {
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return mapper.writeValueAsString(src);
} catch (JsonProcessingException e) {
}
return "";
}
public static T convert(S src, Class pojoClass) throws IOException {
if (src instanceof String) {
return parse((String) src, pojoClass);
}
if (src.getClass().equals(pojoClass)) {
return (T) src;
}
String json = toJson(src);
T target = null;
target = parse(json, pojoClass);
return target;
}
}