Serialization - What are the benefits of using ObjectStreamField [] serialPersistentFields?

For a class that implements the Serializable interface, there are two ways to determine which data streams are transmitted during serialization:

  • By default, all non-static, non-transition fields that implement Serializable are Serializable .
  • ObjectStreamField [] serialPersistentFields defining an ObjectStreamField [] serialPersistentFields and explicitly declaring specific stored fields.

I wonder what is the advantage of the second method, besides the ability to determine the order of individual fields?

+4
source share
4 answers

An โ€œadvantageโ€ is that it does what it says in Javadoc: it determines which fields are serialized. Without it, all non-stationary non-static fields are serialized. Your choice.

+3
source

Fortunately, I'm actually writing this right now ... In addition to the benefits mentioned (and I'm not very good at what I don't understand), writing your own output format seems to have the following advantages:

  • Allows the use of conditional output (various applications for serialization, such as saving and copying, can serialize various parts of an object).
  • It should be faster, use less memory and in some cases use less disk than the default mechanism (this is from Bloch Effective Java 2).
  • Allows you to rename variables in a serialized class, while maintaining backward compatibility.
  • Allows you to access data from deleted fields in the new version (in other words, change the internal representation of your data while maintaining backward compatibility).

I saw the documentation you are quoting, and mentioning only these two options is a little misleading and leaves quite a bit: you can configure your serialization format in two ways, using the ObjectOutput / InputStream interface to write and read the fields in a specific order (described in Bloch) and use the PutField and GetField classes to write and read fields by name. You can use serialPersistentFields as a quote to extend this second method, but this is not required unless you need to read or write data with a name that is not the name of a member variable.

There is a third way to control the format using the Externizable interface, although I have not researched it yet. And some of the benefits can also be obtained through Serialization proxies (see Bloch).

Someone can correct me in detail if I missed something.

+3
source

The advantage is that you can conditionally populate an ObjectStreamField at run time, although only once per JVM life cycle, to determine which fields should be serialized.

 private static final ObjectStreamField [] osf; static { //code to init osf } 
+1
source

In serialPersistentFields you can specify fields that are not necessarily present in the class.

See, for example, the jdk class java.math.BigInteger, where several fields are read and written that no longer exist in the class. These obsolete fields are still read and written for compatibility with older versions. Reading and writing of these fields is processed by the readObject() and writeObject() methods.

See also http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html#6250

0
source

Source: https://habr.com/ru/post/1400901/


All Articles