Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize enum as int

I am looking to return the following class through a web service, which includes an enum type as one of its members.

[Serializable, XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }
}

This works correctly, but will return a result in the form of:

<!-- ... -->
<Accuracy>Unknown or Country or Region or SubRegion or Town or 
  PostalCode or Street or Intersection or Address or Premise</Accuracy>
<!-- ... -->

Instead of a string that represents the enum, I would like it to simply return an integer. Can this be done without changing the type of GeoCoordinate.Accuracy?

like image 631
Karmic Coder Avatar asked Jan 12 '10 21:01

Karmic Coder


People also ask

Can you serialize an enum?

Because enums are automatically Serializable (see Javadoc API documentation for Enum), there is no need to explicitly add the "implements Serializable" clause following the enum declaration. Once this is removed, the import statement for the java. io. Serializable interface can also be removed.

Can enum be serialized C#?

In C#, enums are backed by an integer. Most tools will serialize or save your enums using that integer value.

How are enums represented in JSON?

JSON has no enum type. The two ways of modeling an enum would be: An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values.


2 Answers

Although it is a hack, I deemed using XmlEnumAttribute on each of the enum members to be most pallatable in this case. If this enum were much larger, it would probably be better to use XmlIgnore on the Accuracy property, and add an additional int property to the class as described in another answer to this question.

Usng XmlEnumAttribute means that only the enum itself needs to be modified, and will xml serialize like an int wherever it is used.

    public enum AccuracyLevel
    {
        [XmlEnum("0")] Unknown = 0,
        [XmlEnum("1")] Country = 1,
        [XmlEnum("2")] Region = 2,
        [XmlEnum("3")] SubRegion = 3,
        [XmlEnum("4")] Town = 4,
        [XmlEnum("5")] PostalCode = 5,
        [XmlEnum("6")] Street = 6,
        [XmlEnum("7")] Intersection = 7,
        [XmlEnum("8")] Address = 8,
        [XmlEnum("9")] Premise = 9
    }
like image 142
Karmic Coder Avatar answered Oct 12 '22 21:10

Karmic Coder


I believe you'll need to use [XmlIgnore] on the enum, and create a second property which returns the integer value:

[XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    [XmlIgnore]
    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }

    [XmlElement("AccuracyLevel")]
    public int AccuracyLevelInt
    {
        get {return (int) AccuracyLevel;}
        set {AccuracyLevel = (AccuracyLevel) value;}
    }
}

Note that [Serializable] is not used by the XML Serializer.

Also, note that the AccuracyLevelInt property is probably implemented incorrectly. I'm looking into that now.

like image 36
John Saunders Avatar answered Oct 12 '22 20:10

John Saunders