一个java bean 我知道有两种序列化方案
1、使用Json序列化为字符串
2、使用ObjectOutputStream 序列化为byte[]
不知道各位是使用哪种方式进行序列化存储的?各有什么优缺点
我先抛砖引玉
使用Json序列化
优点:缓存数据便于查阅,都是String字符串
缺点:java bean对象修改后,之前存储的数据,反序列化时会对新增字段赋予默认值,往往会带来业务逻辑上的问题
使用ObjectOutputStream序列化
优点:可以通过serialVersionUID控制版本号,如果bean对象发生修改,反序列化不会成功,能够有效避免上面的缺点
缺点:存储结果不便于查阅
There are many issues that need to be considered during serialization, and there are many serialization solutions to choose from for these issues.
But depending on the scenario, some questions can be ignored.
Here is a good test, I hope it will be helpful to the subject: https://github.com/eishay/jvm-serializer...
org.springframework.data.redis.serializer.JdkSerializationRedisSerializer
The default seems to be this serialization, right? Just use the default one.
For json, deserialization is not very practical for generic collections. Java's own serialization is heavy and slow, which is not ideal, so please consider a serialization framework such as protobuf. In actual use, the effect is still very ideal.