Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename an enum which is serialized to XML but continue to support previous names?

I have an enum which describes options available for a user to select as part of settings. This is serialized to XML. One of the names is not ideal, and I'd like to rename it, but still support deserialization of older settings files.

For example:

public enum Options
{
    Odd,
    NonOdd // rename to 'Even'
}

I know I can rename it but specify the previous serialized name like this:

public enum Options
{
    Odd,
    [XmlEnum(Name = "NonOdd")]
    Even
}

While this works, it continues to use NonOdd in the XML file, which I would prefer not to do.

Is there a way to support deserialization of current and deprecated enum names, but serialize to the current name?

like image 700
JYelton Avatar asked Oct 29 '22 11:10

JYelton


1 Answers

Here's what I would do:

  1. Add a version number to the settings file.
  2. Write code that converts older version XML files to the current version.
  3. Update enum and increase the version number.

Once you have this structure in place you can rename enum elements as many times as you want.

like image 82
atoMerz Avatar answered Nov 09 '22 09:11

atoMerz