Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle conversion from string to enum when string starts with a number in Json.Net

Tags:

json

c#

json.net

I am receiving json from a server which I am converting to an object using Json.Net. For one member I am using the StringEnumConverter which works really perfect.

However all of a sudden the server decided to use strings which also can start with a number which results in a JsonSerilizationException - obviously because enums cannot start with a number in .Net.

Now I am trying to find a solution to handle that.My first approach was to add a "_" when Reading the Json (so my enums in the code would have a starting _ when they are followed by a number) and when writing the json I would delete the starting _ (if a number is following). To achieve this I copied the StringEnumConverter into my namespace and tried to change the according part in the WriteJson and ReadJson methods. However I cannot use the StringEnumConverter since there are other dependencies I cannot access in my own namespace.

Is there any elegant solution to this problem?

like image 436
zlZimon Avatar asked Dec 08 '15 15:12

zlZimon


1 Answers

You can create a JsonConverter and trim the digits from the front

public class Json_34159840
{
    public static string JsonStr = @"{""enum"":""1Value"",""name"":""James"",""enum2"":""1""}";

    public static void ParseJson()
    {
        JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            Converters = new List<JsonConverter> { new EnumConverter() }
        };

        // Later on...
        var result = JsonConvert.DeserializeObject<JsonClass>(JsonStr);
        Console.WriteLine(result.Enum);
        Console.WriteLine(result.Enum2);
        Console.WriteLine(result.Name);
    }
}

public class EnumConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var str = value.ToString();
        if (Regex.IsMatch(str, @"^_"))
        {
            writer.WriteValue(str.Substring(1));
        }
        else
        {
            writer.WriteValue(str);
        }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var value = reader.Value.ToString();
        if (Regex.IsMatch(value, @"^\d+$"))
        {
            return Enum.Parse(objectType, value);
        }

        if (Regex.IsMatch(value, @"^\d+"))
        {
            value = "_" + value;
        }

        return Enum.Parse(objectType, value);
    }

    public override bool CanConvert(Type objectType)
    {
        //You might want to do a more specific check like
        //return objectType == typeof(JsonEnum);
        return objectType.IsEnum;
    }
}

public enum JsonEnum
{
    _0Default,
    _1Value
}

public class JsonClass
{
    public string Name { get; set; }
    public JsonEnum Enum { get; set; }
    public JsonEnum Enum2 { get; set; }
}

Hope this helps.

EDIT: Added support for integers :D

like image 158
sQuir3l Avatar answered Sep 28 '22 17:09

sQuir3l