kecman
kecman

Reputation: 853

(de)serialization of changed objects in C#

I'm using C#, .NET Framework 4.0. I have class which consists of two strings and few int fields. In my program I serialize/deserialize objects of that class, and everything works fine, but in future this class will need to be changed multiple times, by adding new int fields. How can I make old saved data compatible with new objects of same class, for example I need to do an for loop through all objects of that class, including old ones which don't have new int fields set?

Upvotes: 0

Views: 52

Answers (2)

Mark PM
Mark PM

Reputation: 2919

I'd use Json for that. That way when you add new properties to your class you won't need to change any of the load / save code. I usually use json.net for that.

Upvotes: 2

wondra
wondra

Reputation: 3573

How about making the new attributes optional with corrseponding annotation? XML Serialization of the default values of optional attributes

Or you could create code only to migrate between the two versions (load by the old way and convert to new version).

Upvotes: 0

Related Questions