Using readClassDescriptor() and maybe resolveClass() to permit Serialization versioning

前端 未结 5 1950
暗喜
暗喜 2020-12-10 08:11

I am investigating different options in the Java Serialization mechanism to allow flexibility in our class structures for version-tolerant storage (and advocating for a diff

5条回答
  •  时光说笑
    2020-12-10 08:50

    I haven't tinkered with class descriptors much enough, but if your problem is just about renaming and repackaging there is a much easier solution for that. You could just simply edit your serialized data file with a text editor and just replace your old names with new ones. it's there in a human-readable form. for example suppose we have this OldClass placed inside oldpackage and containing an oldField, like this:

    package oldpackage;
    
    import java.io.Serializable;
    
    public class OldClass implements Serializable
    {
        int oldField;
    }
    

    Now when we serialize an instance of this class and get something like this:

    ¬í sr oldpackage.OldClasstqŽÇ§Üï I oldFieldxp    
    

    Now if we want to change class's name to NewClass and put inside newpackage and change its field's name to newField, I simply rename it the file, like this:

    ¬í sr newpackage.NewClasstqŽÇ§Üï I newFieldxp    
    

    and define the appropriate serialVersionUID for the new class.

    That's all. No extending and overriding required.

提交回复
热议问题