简体   繁体   中英

handling Java custom serialization with multiple versions of serializer classes?

I have a custom data storage format I'm implementing on top of either HDF5 or NetCDF. My data format evolves occasionally (but rarely), and it is important to be able to read "old" files later.

I had been using C++ and Microsoft COM but have abandoned those toolkits in favor of Java. In my COM days, I could put the CLSID of the data format reader in the data file in a well-known place, so that if the file was an older (or newer) format than my default file-reader implementation, I could just instantiate the correct implementation if it was installed on my computer. Whenever I up-rev'd the file format, I would release a new reader with the same basic interface but a new CLSID.

Is there an equivalent way to do this in Java?

edit: note — I don't want to serialize the Java object implementations in the data file itself, I just want a way to look up the proper classfile on deserialization. I am not sure if this means I have to change my package name with a version number every time I up-rev the file format, or if I can just use the same class names (eg com.example.customreader) in each version, with some marker like serialVersionUID or an annotation to distinguish the version in question. I'd prefer the latter approach if I can just put multiple versions of jar files in my classpath.

edit 2: My data is stored in a way that has nothing to do with ObjectInputStream and Serializable and I have no plans to do so unless there is some advantage. I handle the reading/writing on my own and because of the features/characteristics of HDF5/NetCDF, it's not just as simple as dealing with an ObjectInputStream.

我对COM不熟悉,但是您可能想看看serialVersionUID的文档-当序列化应该更改时,可以修改它。

I think you should take a look at Versioning of Serializables Objects section

If you want to have more control, you can call a remote method to return the proper serializer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM