- 对象序列化机制:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。
- 序列化操作的对象需要实现Serializable或Externalizable,并且其内部所有属性也必须是可序列化的(String和StringBuilder类已实现了Serializable,基本数据类型默认可序列化),因为static和transient修饰的成员变量,不会被操作,所以不能序列化。
- 一般都是把对象转为josn格式进行序列化
自定义类举例:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Ahzoo implements Serializable {//需要将类实现Serializable或Externalizable才能进行对象的序列化操作
// 实现Serializable接口后,需要提供一个long类型的全局常量,值可以任意数,但是必须声明,反序列时会进行对比,如果serialVersionUID一致,会认为是一致的,否则就会报序列号不一致的异常
public static final long serialVersionUID = 7777777L;{
private Integer age;
private String name;
private Account account;
}
class Account implements Serializable{//Account作为Person类的属性,也必须是可进行序列化的才行
public static final long serialVersionUID = 999999999L;
int id;
public Account(int id){
this.id = id;
}
}
FastJSON
引入依赖:
<!-- json与对象互转-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
测试使用:
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class MethodTest {
@Test
public void toTest {
//转成json字符串
String json = JSON.toJSONString(new StringBuilder("ahzoo"));
System.out.println(json);
//转成(StringBuilder)对象
StringBuilder str = JSON.parseObject(json,StringBuilder.class);
System.out.println(str);
//转成(StringBuilder)列表
List<StringBuilder> = JSONObject.parseObject(json, new TypeReference<List<StringBuilder>>() {
});
//转为Map
List<Map<String, Object>> listMap = JSON.parseObject(json, new TypeReference<List<Map<String,Object>>>(){});
}
Jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
测试使用:
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
public class MethodTest {
@Test
public void toTest {
ObjectMapper objectMapper = new ObjectMapper();
try {
//转为JSON
String json = objectMapper.writeValueAsString(new StringBuilder("ahzoo"));
//转为对象
StringBuilder str = objectMapper.readValue(json, StringBuilder.class);
//转为List
List<StringBuilder> = objectMapper.readValue(fileString, new TypeReference<List<StringBuilder>>() {
});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
测试使用:
import com.google.gson.Gson;
import org.junit.Test;
public class MethodTest {
@Test
public void toTest {
//转成json字符串
Gson gson = new Gson();
String json = gson.toJson(new StringBuilder("ahzoo"));
System.out.println(json);
//json字符串转成对象
StringBuilder str = gson.fromJson(json,StringBuilder.class);
System.out.println(str);
//转为list
List<StringBuilder> list = gson.fromJson(json, new TypeToken<List<StringBuilder.class>>() {
}.getType());
}
}
后记
- 使用FastJSON序列化时,栈内存溢出:
原因:需要被反序列化的实体类继承了一个类(DataEntity),该类中的一个递归方法陷入了死循环,最终导致栈内存溢出java.lang.StackOverflowError: null
at org.nustaq.serialization.FSTObjectInput$2.readObjectOverride(FSTObjectInput.java:1030)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:365)
at org.nustaq.serialization.FSTObjectInput$MyObjectStream.readObjectOverride(FSTObjectInput.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:365)
at java.util.HashMap.readObject(HashMap.java:1396)
at sun.reflect.GeneratedMethodAccessor174.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.nustaq.serialization.FSTObjectInput.readObjectCompatibleRecursive(FSTObjectInput.java:609)
at org.nustaq.serialization.FSTObjectInput.readObjectCompatibleRecursive(FSTObjectInput.java:598)
at org.nustaq.serialization.FSTObjectInput.readObjectCompatibleRecursive(FSTObjectInput.java:598)
at org.nustaq.serialization.FSTObjectInput.readObjectCompatible(FSTObjectInput.java:574)
at org.nustaq.serialization.FSTObjectInput.instantiateAndReadNoSer(FSTObjectInput.java:559)
at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:374)
at org.nustaq.serialization.FSTObjectInput.readObjectFields(FSTObjectInput.java:713)
at org.nustaq.serialization.FSTObjectInput.instantiateAndReadNoSer(FSTObjectInput.java:566)
at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:374)
at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311)
at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245)
at org.nustaq.serialization.FSTConfiguration.asObject(FSTConfiguration.java:1158)
at com.jeesite.common.lang.ObjectUtils.unserializeFst(ObjectUtils.java:242)
at com.jeesite.common.lang.ObjectUtils.cloneBean(ObjectUtils.java:259)
at com.jeesite.common.entity.BaseEntity.clone(dk:489)
at com.jeesite.modules.sys.utils.UserUtils.get(nd:7)
at com.jeesite.modules.sys.utils.UserUtils.getUser(nd:396)
at com.jeesite.common.entity.BaseEntity.getCurrentUser(dk:542)
at com.alibaba.fastjson.serializer.ASMSerializer_2_User.write(Unknown Source)
经过多方调试后发现,类(DataEntity
)中的(getCurrentUser()
)方法的currentUser
属性在递归中一直都是null
,最终导致栈内存溢出。
(我的)解决方法:将FastJSON替换为Jackson;
替换后再次调试,发现currentUser
属性被成功赋值