How does Java's serialization work and when it should be used instead of some other persistence technique?

前端 未结 9 934
失恋的感觉
失恋的感觉 2020-11-30 20:54

I\'ve been lately trying to learn more and generally test Java\'s serialization for both work and personal projects and I must say that the more I know about it, the less I

相关标签:
9条回答
  • 2020-11-30 21:23

    The main advantage of serialization is that it is extremely easy to use, relatively fast, and preserves actual Java object meshes.

    But you have to realize that it's not really meant to be used for storing data, but mainly as a way for different JVM instances to communicate over a network using the RMI protocol.

    0 讨论(0)
  • 2020-11-30 21:26

    The reason why storing a tiny amount of information is serial form is relatively large is that it stores information about the classes of the objects it is serialising. If you store a duplicate of your list, then you'll see that the file hasn't grown by much. Store the same object twice and the difference is tiny.

    The important pros are: relatively easy to use, quite fast and can evolve (just like XML). However, the data is rather opaque, it is Java-only, tightly couples data to classes and untrusted data can easily cause DoS. You should think about the serialised form, rather than just slapping implements Serializable everywhere.

    0 讨论(0)
  • 2020-11-30 21:32

    If you don't have too much data, you can save objects into a java.util.Properties object. An example of a key/value pair would be user_1234_firstname = Peter. Using reflection to save and load objects can make things easier.

    0 讨论(0)
提交回复
热议问题