-
Feb 5th, 2018, 11:19 AM
#1
Thread Starter
New Member
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?
-
Feb 5th, 2018, 11:43 AM
#2
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.
-
Feb 5th, 2018, 08:59 PM
#3
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
-
Feb 6th, 2018, 08:05 AM
#4
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.
-
Feb 6th, 2018, 08:58 AM
#5
Thread Starter
New Member
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
-
Feb 6th, 2018, 10:29 AM
#6
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
-
Feb 6th, 2018, 10:36 AM
#7
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.
-
Feb 6th, 2018, 11:14 AM
#8
Re: Serializable Class update
Originally Posted by Sitten Spynne
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
-
Feb 6th, 2018, 11:54 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|