none
Binary deserialization of a changing structure RRS feed

  • General discussion

  • Suppose I have a structure serialized in binary format and stored on disk. If I later add or remove some variable from the structure, errors occur when deserializing the old structure stored on the disk.

    How is it solved so that different versions can easily coexist?
    • Changed type CoolDadTxModerator Wednesday, July 18, 2018 1:31 PM User isn't asking a question they want answered
    Tuesday, July 17, 2018 11:28 AM

All replies

  • Check that it actually already works.

    In case of adding new members, check this article:


    Example of serialisation of first version:

    [Serializable]

    public class MyClass

    {

       public int Member1;

    }

    . . .

    var example = new MyClass { Member1 = 1234 };

    var bf = new BinaryFormatter();

    using( var fs = File.OpenWrite( "MyFile.data" ) )

    {

           bf.Serialize( fs, example );

    }

     

     

    Deserialisation in new version:

     

    [Serializable]

    public class MyClass

    {

       public int Member1;

       [OptionalField]

       public int Member2;

       [OnDeserializing]

       private void SetDefaults( StreamingContext sc )

       {

          Member2 = 5678; // default value in case of old files

       }

    }

    . . .

    MyClass example;

    var bf = new BinaryFormatter();

    using( var fs = File.OpenRead( "MyFile.data" ) )

    {

       example = (MyClass)bf.Deserialize( fs );

    }

     

     

    You can also implement the ISerialisable interface, i.e. GetObjectData for serialisation and a special constructor for deserialisation:



    • Edited by Viorel_MVP Tuesday, July 17, 2018 3:42 PM
    Tuesday, July 17, 2018 3:42 PM
  • If you're using binary serialization then add a version header. The serializer may always write using the latest version but the deserializer will user the version to determine which version of the data is being read. It then has to convert that to the current version.

    If you're using BinaryFormatter then refer to this article on version tolerant serialization.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 17, 2018 5:48 PM
    Moderator
  • It does not work because it is not practical. Also you can not delete variables or change their type. Result: binary serialization can not be used.
    Wednesday, July 18, 2018 4:08 AM
  • Please clarify what doesn't work. I've used versioned structures for years and changing members (or using a completely new structure) works just fine.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 4:23 AM
    Moderator
  • What I just said.
    Wednesday, July 18, 2018 4:29 AM
  • If you are expecting anyone here to help you then please provide more information such as example code, issues you're having, etc. As I said others aren't having versioning issues with binary serialization but we may be using it differently than you. Showing us how you're reading/writing your structures would allow us to better assist you.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 5:00 AM
    Moderator
  • What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.What I just said.
    Wednesday, July 18, 2018 5:12 AM
  • Internal message for Microsoft employees: when someone finds flaws in our software, you have to say "I have been using it for years and it works perfectly for me. Please clarify your questions better." And so the time passes happily .... jejejejejeeeeee

    Example: https://social.msdn.microsoft.com/Forums/vstudio/en-US/6e1cb5ed-60fb-4d27-af97-9d31c8691139/sql-server-2017-with-transact-and-synonyms-problems-microsoft-does-not-respond?forum=transactsql

    Wednesday, July 18, 2018 5:26 AM
  • It is clear from this thread and the others you've made that you're not actually coming to the forums to seek help but rather complain about MS products. I'm afraid this isn't the place for that. The bulk of the users on these forums are other professionals trying to help you, not MS employees. If you want to provide feedback to MS that they may see you should be posting over in UserVoice, DeveloperCommunity or GitHub.

    I'm changing this thread to a discussion since you're not actually asking a question that you intend to be answered.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 1:31 PM
    Moderator