Click here to Skip to main content
14,697,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I need to deserialize a json with various colour shades to primary colour enums -
Eg. Scarlet and Crimson to enum Red (0), and Teal and Lime to Green (1) - ,but can't work out how , or if it's possible.

The code might look something like this:

using System.Runtime.Serialization;
public enum PrimaryColour
{
     [EnumMember(Value = "Scarlet"), EnumMember(Value = "Crimson")]
     Red = 0,

     [EnumMember(Value = "Teal"), [EnumMember(Value = "lime")]]
     Green=1
}


Any suggestions?

Thanks

Andy

What I have tried:

I've tried varients of the example enum, but all were invalid.
Posted
Updated 17-Jul-19 4:42am
v3

According to EnumMemberAttribute Class[^], this attribute is defined as:
[AttributeUsage(AttributeTargets.Field, AllowMultiple=false, Inherited=false)]
public sealed class EnumMemberAttribute : Attribute
This means that you cannot match several names to the same enumeration value.
What you can do is provide a method which will return a PrimaryColour value from a string:
public static PrimaryColour GetColour(string value)
{
   switch (value.ToLowerInvariant())
   {
      case "crimson":
      case "scarlet":
         return PrimaryColour.Red;
      case "teal":
      case "lime":
         return PrimaryColour.Green;
      default:
         return PrimaryColour.Black;
   }
}

Then, during the deserialization process, hook it to assign a correct value from json (this part depends on the way the json deserialization is handled, which I have no knowledge of at the moment).

Hope this helps.
   
v2
Thanks for your help, Phil. I can see where you're going with this, but I'm unclear as to how to implement this is the context of reading a file (html get).
Take the following example .This works fine for crimson cars, but I also need scarlet cars to deserialize as Red. Thoughts?

using System;
using System.IO;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DeserializeObject();
        }

        class CarModel
        {
            public ColourEnum colour;
        }

        enum ColourEnum
        {
            //[EnumMember(Value = "Scarlet")]
            [EnumMember(Value = "Crimson")]
            Red,
            //[EnumMember(Value = "Lime")]
            [EnumMember(Value = "Teal")]
            Green,
        }

        static void DeserializeObject()
        {
            using (Stream reader = new FileStream("c://tmp//test.json", FileMode.Open))
            {
                using (var sr = new StreamReader(reader))
                {

                    using (var jr = new JsonTextReader(sr))
                    {
                        var serializer = new JsonSerializer();
                        CarModel car = serializer.Deserialize<CarModel>(jr);

                        Console.Write(car.colour);
                    }
                }
            }
        }
    }

    /*
    c://tmp//test.json = 

    {
    "colour" : "Crimson"
    }

    */
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900