提示:不多哔哔,直接cv
一、引入依赖
1.maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
2.代码部分
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Collection;
/**
* 序列化/反序列工具
*
* @author wcc
* @date 12/24/2019 4:11 PM
*/
public class SerializeUtil {
private static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final ObjectMapper OBJECT_MAPPER;
private static final XmlMapper XML_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
XML_MAPPER = new XmlMapper();
XML_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
/**
* 将json反序列化为对象
*
* @param <T>
* @param json
* @param t
* @return
*
* @author wcc
* @date Dec 28, 2019 2:13:32 PM
*/
public static <T> T deserialization(String json, Class<T> t) {
try {
T t1 = OBJECT_MAPPER.readValue(json, t);
return t1;
} catch (Exception e) {
LOG.error("Json convert to {} object exception: {}, json: {}", t.getClass(), ExceptionUtils.getStackTrace(e), json);
throw new RuntimeException(e);
}
}
/**
* JSON反序列化成泛型集合
*
* @param <T>
* @param json
* @param collectionClass
* @param elementClasses
* @return
*
* @author wcc
* @date Dec 28, 2019 2:13:44 PM
*/
public static <T> T deserialization(String json, Class<?> collectionClass, Class<?>... elementClasses) {
try {
JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
T t1 = OBJECT_MAPPER.readValue(json, javaType);
return t1;
} catch (Exception e) {
LOG.error("Json convert to {} object exception: {}, json: {}", collectionClass.getClass(), ExceptionUtils.getStackTrace(e), json);
throw new RuntimeException(e);
}
}
/**
* 序列化
*
* @param <T>
* @param t
* @return
*
* @author wcc
* @date Dec 28, 2019 2:13:54 PM
*/
public static <T> String serialize(T t) {
try {
return OBJECT_MAPPER.writeValueAsString(t);
} catch (IOException e) {
LOG.error("Object {} to json exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
/**
*
*
* @param <T>
* @param t
* @param features
* @return
*
* @author wcc
* @date Dec 28, 2019 2:14:07 PM
*/
public static <T> String serialize(T t, SerializationFeature... features) {
try {
ObjectWriter writer = null;
if (features != null && features.length > 0) {
writer = OBJECT_MAPPER.writer(features[0], Arrays.copyOfRange(features, 1, features.length));
} else {
writer = OBJECT_MAPPER.writer();
}
return writer.writeValueAsString(t);
} catch (IOException e) {
LOG.error("Object {} to json exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
/**
* 将xmlString 转换为 指定的对象
*
* @param <T>
* @param xmlString
* @param t
* @return
*
* @author wcc
* @date Dec 28, 2019 2:14:15 PM
*/
public static <T> T xmlToObject(String xmlString, Class<T> t) {
try {
return XML_MAPPER.readValue(xmlString, t);
} catch (IOException e) {
LOG.error("Xml convert to {} object exception: {}, xml: {}", t.getClass(), ExceptionUtils.getStackTrace(e), xmlString);
throw new RuntimeException(e);
}
}
public static <T> T xmlToObject(InputStream xmlInputStream, Class<T> t) {
try {
return XML_MAPPER.readValue(xmlInputStream, t);
} catch (IOException e) {
LOG.error("Xml inputStream convert to {} object exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
public static <T> T xmlToObject(byte[] xmlbytes, Class<T> t) {
try {
return XML_MAPPER.readValue(xmlbytes, t);
} catch (IOException e) {
LOG.error("Xml byte[] convert to {} object exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
@SuppressWarnings("rawtypes")
public static <T> T xmlToObject(String xmlString, Class<? extends Collection> collectionClass,
Class<T> elementClass) {
try {
JavaType javaType = getCollectionJavaType(collectionClass, elementClass);
return XML_MAPPER.readValue(xmlString, javaType);
} catch (IOException e) {
LOG.error("Xml convert to {} object exception: {}", collectionClass.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
@SuppressWarnings("rawtypes")
public static <T> T xmlToObject(InputStream xmlInputStream, Class<? extends Collection> collectionClass,
Class<T> t) {
try {
JavaType javaType = getCollectionJavaType(collectionClass, t);
return XML_MAPPER.readValue(xmlInputStream, javaType);
} catch (IOException e) {
LOG.error("Xml inputStream convert to collection {} object exception: {}", collectionClass.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
@SuppressWarnings("rawtypes")
public static <T> T xmlToObject(byte[] xmlbytes, Class<? extends Collection> collectionClass, Class<T> t) {
try {
JavaType javaType = getCollectionJavaType(collectionClass, t);
return XML_MAPPER.readValue(xmlbytes, javaType);
} catch (IOException e) {
LOG.error("Xml byte[] convert to collection {} object exception: {}", collectionClass.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
/**
* 将java bean序列化为String
*
* @param <T>
* @param t
* @return
*
* @author wcc
* @date Dec 28, 2019 2:14:32 PM
*/
public static <T> String objectToXml(T t) {
try {
return XML_MAPPER.writeValueAsString(t);
} catch (JsonProcessingException e) {
LOG.error("Object {} convert to xml exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
public static <T> String objectToXml(T t, SerializationFeature... features) {
ObjectWriter writer;
if (features != null && features.length > 0) {
if (features.length == 1) {
writer = XML_MAPPER.writer(features[0]);
} else {
writer = XML_MAPPER.writer(features[0], Arrays.copyOfRange(features, 1, features.length));
}
} else {
writer = XML_MAPPER.writer();
}
try {
return writer.writeValueAsString(t);
} catch (JsonProcessingException e) {
LOG.error("Object {} convert to xml exception: {}", t.getClass(), ExceptionUtils.getStackTrace(e));
throw new RuntimeException(e);
}
}
public static ObjectMapper getMapper() {
return OBJECT_MAPPER;
}
@SuppressWarnings("rawtypes")
private static <T> JavaType getCollectionJavaType(Class<? extends Collection> collectionClass,
Class<T> elementClass) {
return XML_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClass);
}
}
总结
有点东西,但不多