Java序列化与反序列化
一、概念:
1、Java序列化:把Java对象转换为字节序列的过程
2、Java反序列化:将字节序列恢复为Java对象的过程
二、用途:
1、想要将Java对象从内存中保存到一个文件中或者数据库中时候
2、想要将Java对象在网络上传递的时候
3、通过序列化在进程间传递对象
Serializable序列化接口属于java.io下的类,所以序列化存储或者序列化传递的是一个字节流。
三、实现:
package com.sigma.sigmacore.utils; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import static com.google.common.base.Preconditions.checkNotNull; public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); //父类的静态变量->父类的静态初始化块->子类的静态变量->子类的静态初始化块 static { //默認忽略位置屬性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public static ObjectMapper getMapper() { return objectMapper; } /** * 將對象序列化為JSON * * @param object 對象 * @return json字符串 * @throws IOException IO異常(包含JSON異常) */ public static String serialize(Object object) throws IOException { return serialize(object, false); } /** * 將對象序列化為JSON * * @param object 對象 * @param withPretty 是否格式化 * @return json字符串 * @throws IOException IO異常(包含JSON異常) */ public static String serialize(Object object, boolean withPretty) throws IOException { Writer write = new StringWriter(); if (withPretty) { objectMapper.writerWithDefaultPrettyPrinter().writeValue(write, object); } else { objectMapper.writeValue(write, object); } return write.toString(); } /** * 將對象序列化為JSON * * @param objectMapper 自配置對象 * @param object 對象 * @param withPretty 是否格式化 * @return Json字符串 * @throws IOException IO異常 */ public static String serialize(ObjectMapper objectMapper, Object object, boolean withPretty) throws IOException { Writer write = new StringWriter(); if (withPretty) { objectMapper.writerWithDefaultPrettyPrinter().writeValue(write, object); } else { objectMapper.writeValue(write, object); } return write.toString(); } /** * 反序列化為T * * @param json json字符串 * @param classType 類別 * @param <T> 泛型 * @return 對象 * @throws IOException IO異常(包括JSON異常) */ public static <T> T deserialize(String json, Class<T> classType) throws IOException { checkNotNull(json); return (T) objectMapper.readValue(json, TypeFactory.rawClass(classType)); } /** * 反序列化為T * * @param json json字符串 * @param typeRef 類別 * @param <T> 泛型 * @return 對象 * @throws IOException IO異常(包括JSON異常) */ public static <T> T deserialize(String json, TypeReference<T> typeRef) throws IOException { return objectMapper.readValue(json, typeRef); } /** * 反序列化為T * * @param objectMapper 自配置Mapper * @param json json字符串 * @param typeRef 類型 * @param <T> 泛型 * @return 對象 * @throws IOException IO異常(包括JSON異常) */ public static <T> T deserialize(ObjectMapper objectMapper, String json, TypeReference<T> typeRef) throws IOException { return objectMapper.readValue(json, typeRef); } }
llh