1

I am new to this. In my model class I have

public class Status
{
   public Codes CodeStatus { get; set; }
   public enum Codes
   {
      Unknown = 0,
      Green=1,
      Yellow=2,
      Red =3
   }
}

Now when I use this in my controller like this

int dbStatus = 3;
Status oStatus = new Status();
oStatus.CodeStatus = (Status.Codes)dbStatus ;
List<Status> ListStatus = new List<Status>();
ListStatus.Add(oStatus);
return ListStatus.ToList();

When I debug the value of oStatus.CodeStatus under quick watch it shows "Red" but when JSON renders in postman, the value comes up in integer rather than "Red". How can Json render it in red, green or yellow values in the list.

JSON looks like this

"FD": [
         {
            "Id": 416308,
            "Name": "Head Office ",       
            "CodeStatus": 3,

         }
      ]
Sarah
  • 1,199
  • 2
  • 21
  • 42
  • Possible duplicate of [.NET - JSON serialization of enum as string](https://stackoverflow.com/questions/2441290/net-json-serialization-of-enum-as-string), see also [Serialize a container of enums as strings using JSON.net](https://stackoverflow.com/q/18640162/1271037) – dovid Feb 28 '19 at 22:51

1 Answers1

2

Under the hood, an enum is just a set of named constants whose underlying type is an integer. The json deserializer knows how to convert its integer value to its enum value.

When you ask:

How can Json render it in red, green or yellow values in the list.

I'm assuming you want your "FD" array to contain an array of objects, each with a CodeStatus key that has a string value, instead of an int value? For example, you would like to see this:

"FD": [
         {
            "Id": 416308,
            "Name": "Head Office ",       
            "CodeStatus": "Red",
         }
      ]

Correct?

If so, you could add a second property to that class that is a string version of the CodeStatus enumeration. And because it's a "derived" property, you can use either an "Expression Bodied Function" (aka fat arrow) or a read-only property (aka a property with only a getter). So something like:

public class Status
{
   public string CodeStatus => CodeStatusEnum.ToString();
   //Optionally add 
   //[JsonIgnore] // to exclude it from Json Serialization
   public Codes CodeStatusEnum { get; set; }
   //Could also be
   //public string CodeStatus { get { return CodeStatusEnum.ToString(); } }
   public enum Codes
   {
      Unknown = 0,
      Green=1,
      Yellow=2,
      Red =3
   }
}

Lastly, if you didn't want the non-string property to show up at all in the serialized JSON, you could add a [JsonIgnore] attribute to it.

tobyb
  • 696
  • 9
  • 18
  • Sorry I confused you, Debugger listed property value is red for dbStatus = 3 in my code above. So when I hover over or watch value for ListStatus.ToList() i see red for dbstatus 3. The Jason listed is only an example as there are more values in the list too. CodeStatus in the JSON would be 3 for dbStatus =3. – Sarah Mar 01 '19 at 17:28
  • 1
    Updated my answer. I think I answered your question. Please let me know if not. – tobyb Mar 02 '19 at 00:15
  • That worked like a charm, thank you so much. I think I learned much by your help on this post. Thank you. – Sarah Mar 04 '19 at 18:59