Search code examples
c#unity-game-engineserializationpersistencebinaryformatter

BinaryFormatter alternative to support seamless model versions?


I like BinaryFormatter, it is lightweight (2 lines of code). But I can feel it carries some complication risk.

Especially when it comes to different application versions with slightly different models (renamed / removed / moved properties / classes across versions).

What is the best alternative?

Considering lightweight, low-boilerplate, but with some option to manage versions properly without much overhead.


Solution

  • Based on your description, you are violating the rules of backwards compatibility. It will be hard if not impossible to hand those cases in binary serialization.

    Never remove a serialized field
    Never change the name or type of a serialized field
    When adding a new field, mark it with the OptionalField attribute
    

    If you want to handle every single case under the sun where fields can change types, names, location, etc. You'd be better off with Json or XML where the first tag is required to be the version and it can never change. Then you'll know how to process the rest of the file.

    You can do the same thing with binary serialization, but not in any automated fashion, and you'll end up with code like:

    readByte()
    If ver 1 readInt()
    If ver 2 readInt(), readString()
    readArray()
    If ver 4 readBool()
    If ver 3 readDouble()
    

    Etc. if you don't want to follow the above rules about version tolerance, binary shouldn't be your first choice.