I am currently serializing a class that contains dictionaries likes this:
[DataMember]
public Dictionary<SomeEnum, SomeData<long>> SomeDictionary = new Dictionary<SomeEnum, SomeData<long>>();
That works fine, but in the future I might want to remove a value from the enum. Currently I cannot deserialize data anymore if I remove an enum value. How do I instruct the DataContractSerializer to skip the dictionary entry if the key cannot be deserialized?
By changing the enum you are changing the contract - not a good practice (Change enum change contract)
If you want to go ahead, you'll have to implement custom serialization (e.g. Custom JSON Serialization of enum) and use the fact that .Net type Enum
supports method IsDefined()
...
enum myEnum { ev1, ev3 };
:
:
Console.WriteLine($"{Enum.IsDefined(typeof(myEnum), "ev1")} {Enum.IsDefined(typeof(myEnum), "ev2")}");
giving
True False