recoup8063
recoup8063

Reputation: 4280

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?

Upvotes: 0

Views: 418

Answers (2)

woot
woot

Reputation: 2139

Have you tried using Json.NET? It is tolerant of extra properties (along with being much faster and having other added features).

I just tested with (saving with bar and baz, then removing baz, and deserializing):

public class foo {
    public int bar {get;set;}
    public int baz {get;set;}
}

Upvotes: 1

Chris Hinton
Chris Hinton

Reputation: 866

I believe you looking for Version Tolerant Serialization... Read about it here.

Upvotes: 1

Related Questions