- Home /
Unity C# Deserialization dealing with extra properties
I am attempting to save data about my game made in Unity, written in C#. I am able to serialize my data and deserialize it. The problem comes when I have deserialize a file and the class I serialized has had a property removed. When this happens a SerializationException
is thrown. Example: Lets say I have this initial class:
[Serializable]
public class foo {
public int bar;
public int baz;
}
Then I serialize it to a file. I then modify this class and remove public int baz
. Next time I try and deserialize that file it will throw an exception because it can't find a spot to put baz in class foo.
Is there a way I can tell the deserializer to ignore extra properties or to cast class foo as something that can handle extra objects?
Answer by Xzemplar · Jun 09, 2015 at 09:56 PM
As far as I know, this has been a major issue with serialization in general for a long time.
To get around this there are a number of options, one of which is to have your serialized stream include a version number, and when you release a new version have a version checker function convert your old serialized class to the new format filling in blanks with defaults (for each class you have modified). it helps if you only add members to the end of the members list when you make new ones (and dont remove members if you can help it or you will have to do more work on the file version conversion) and use default constructors thouroughly
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Error Deserializing Dictionary 1 Answer
Multiple Cars not working 1 Answer
Sprites doens't collide properly or bypass each other 0 Answers