Deserialize json character as enumeration

SelAromDotNet picture SelAromDotNet · Aug 31, 2013 · Viewed 26k times · Source

I have an enumeration defined with C#, where I'm storing it's values as characters, like this:

public enum CardType
{
    Artist = 'A',
    Contemporary = 'C',
    Historical = 'H',
    Musician = 'M',
    Sports = 'S',
    Writer = 'W'
}

I'm attempting to deserialize using JSON.NET, but the incoming JSON was written using the CHAR value (string) instead of the int value of the enumeration, like this:

[{"CardType","A"},{"CardType", "C"}]

Is it possible to define some kind of converter that will allow me to manually parse the char to the enum value?

I tried creating a JsonConverter, but am not sure how to do it, while applying it only to this property and not the whole parsed object. here's what I tried:

public class EnumerationConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return null;
        }

        int value = serializer.Deserialize<int>(reader);
        return (CardType)value;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsSubclassOf(typeof(string));
    }
}

The logic might be wrong and I can fix that but the problem is ReadJson() isn't being called at all.

CanConvert is, but it appears to be called for every property, not just the one property I defined it for:

public class Card
{
            private CardType type;
        [JsonConverter(typeof(EnumerationConverter))]
        public CardType Type
        {
            get { return type; }
            set { type = value; }
        }
}

I'm sure I've done this incorrectly but am having trouble finding documentation on how to do this for a single field...

What am I missing?

Answer

nemesv picture nemesv · Aug 31, 2013

You don't necessary need a custom JsonConverter you can use the built in StringEnumConverter with the combination of the EnumMemberAttribute (from the System.Runtime.Serialization assembly).

Without the EnumMemberAttribute it uses the enum names so Artist, Contemporary, etc so you need to change the names with it to your A,C, etc value.

But it is not the nicest solution because you have to repeat your values two times, but it works:

[JsonConverter(typeof(StringEnumConverter))]
public enum CardType
{
    [EnumMember(Value = "A")]
    Artist = 'A',
    [EnumMember(Value = "C")]
    Contemporary = 'C',
    [EnumMember(Value = "H")]
    Historical = 'H',
    [EnumMember(Value = "M")]
    Musician = 'M',
    [EnumMember(Value = "S")]
    Sports = 'S',
    [EnumMember(Value = "W")]
    Writer = 'W'
}