Results 1 to 9 of 9

Thread: Serializable Class update

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Serializable Class update

    Hello all,
    I have created a serializabel calss with many properties. I used this class to store some data on HD. Now I need to add some more properties to the calss and I need to open the older files with it. Like new verision of the file. The hard way is to use the old and the new class in the application and copy propertie after propertie from the old object into the object created by the new class. This would take a lots of code. Is there a better way hot to upgrade the older object?

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Serializable Class update

    My interpretation of the documentation is that only BinaryFormatter and a worse class have version-tolerant serialization. If that's what you're using, it'll ignore new properties and leave them at a default version. Your code should tolerate that.

    If you're using XML serialization, I don't see any indications that it is or isn't version tolerant. Have you tried it and failed or are you asking in advance?

    If you tried it and failed, you could write a custom XML parser that parses the old version if trying to parse a new version fails. Maybe try putting OptionalFieldAttribute on the new properties. If that doesn't work, well, this is your option.

    If XML serialization really doesn't support this, it looks like you can use Data Contracts going forward to do your serialization if you want to spend a few extra hours on configuration.

    Personally, I use the Newtonsoft JSON package for serialization these days. By default, it's version-tolerant and will ignore both missing and unexpected members. You can configure it to be more or less strict if you desire, and JSON is a far more standard format for data transfer than XML today.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Serializable Class update

    I've never had a problem serializing/deserializing when using XML when adding/removing properties... I HAVE had the issue with Binary serialization in this regard. As long as you're adding properties, and using XML, you shouldn't have any problems.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Serializable Class update

    I don't disagree, but haven't tested. My gut tells me XML serialization should behave like I expect JSON serialization to behave. I tried to find out from documentation but version tolerance for XML serialization is undefined. ��*♂️
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Serializable Class update

    I am using Binary serialization. My code tolerates the older object, but I cannot add new properties when I open the older object. The problem is that I have new version of the application, I need to open the files from the previous version and save it as new version. The new version of the object does not change anything in the previous object (all the properties are the same) just add more properties to it.

    Toma

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Serializable Class update

    If you're adding properties, then the properties are not the same. What you'll need to do is provide a conversion step... load the data using the old, unmodified class... then copy the values to the new class with the additional propertied, then re-serialize it back to the file.
    It sucks I know. But it's also the only way I've been able to get it to work fool proof with binary serialization. Unless someone knows something I don't and there's a better way to skin this cat.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Serializable Class update

    Have a look at this article.

    It looks like you should mark any properties you add with the OptionalFieldAttribute, so the serializer knows it's OK if they aren't present. If you need to initialize them to some default value, you can add a callback and mark it with the OnDeserializing attribute so you can set the appropriate default values. Pay close attention to the rules at the bottom: removing properties is not supported in this serialization engine.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Serializable Class update

    Quote Originally Posted by Sitten Spynne View Post
    Have a look at this article.

    It looks like you should mark any properties you add with the OptionalFieldAttribute, so the serializer knows it's OK if they aren't present. If you need to initialize them to some default value, you can add a callback and mark it with the OnDeserializing attribute so you can set the appropriate default values. Pay close attention to the rules at the bottom: removing properties is not supported in this serialization engine.
    Nice... where the heck was this when I could have used used it? :/

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Serializable Class update

    Great, this is exactly what I was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width