Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Serialization And Versioning

Discussion in 'Scripting' started by steviebob, Oct 12, 2014.

  1. steviebob

    steviebob

    Joined:
    Jul 15, 2014
    Posts:
    21
    Hi Guys,

    I'm using the .NET BinaryFormatter class to serialize and deserialize my object which contains player data.
    I noticed that I misspelled a variable, so I corrected it.
    Next time I loaded the game, when it tried to deserialize the player data that was previously saved, it threw a serialization exception, stating that it couldn't find the misspelled version that I had recently corrected.

    My question is this:
    How do you recommend I handle serialization versioning?
    Should I keep all of the old properties forever, even if they're unused and just map them to new properties where I still want to use them?
    That will bloat the code a bit if I were to have a large object stored with many properties that evolve over time.

    I've found a reasonably clear article from microsoft:
    http://msdn.microsoft.com/en-us/library/ms229752(v=vs.110).aspx

    But it only really caters for adding additional properties to an existing class, and recommends against ever removing a field.
    So the code would still get bloated and slightly more difficult to maintain.
    Is there really no other way that would make sense?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    If you use ISerializable, you kinda handle manually entry and exit of your serialization. That way you can insert a kind of translator in the deserialization process and redirect towards the new field.
     
  3. steviebob

    steviebob

    Joined:
    Jul 15, 2014
    Posts:
    21
    Thanks, I'll give it a try.
    It certainly looks more flexible, although it looks like it requires more work to implement (Which is to be expected).

    Are there any "gotcha's" and or limitations that I should be aware of?

    My existing implementation of deserializing the object is as follows:

    BinaryFormatter bf = new BinaryFormatter();
    using (FileStream fs = File.Open(Application.persistentDataPath + "/PlayerInfo.dat", FileMode.Open))
    {
    PlayerData = (PlayerData)bf.Deserialize(fs);
    }

    Would the above implementation still work and it would just be the "PlayerData" class itself that I'd have to adjust?
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Most likely.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I found dealing with serialization through ISerializable to be a greater nuisance than I wanted. I've switched to a clean, simple text-based format — though that was in part because I also wanted a human-editable format for mod support.

    You can read more about it on the High Frontier Blog. And if you want to try the GRFON format for your own game, just shoot me a PM. It's a single file of code, and in fairly decent shape by this point; I'm happy to share.
     
    wlwl2 likes this.