1

I have a status property with int values in array of objects. Below is the output of array of objects.

[
    {
        "enterpriseServiceId": 1,
        "enterpriseServices": {},
        "status": 3,
        "id": 1,
        "createdOn": "2017-12-29T17:58:15.4855946",
        "createdBy": "System",
        "modifiedOn": "2017-12-29T17:58:15.4855946",
        "modifiedBy": "System"
    },
    {
        "enterpriseServiceId": 2,
        "enterpriseServices": {},
        "status": 1,
        "id": 2,
        "createdOn": "2017-12-29T17:58:15.4855946",
        "createdBy": "System",
        "modifiedOn": "2017-12-29T17:58:15.4855946",
        "modifiedBy": "System"
    }
]

The status property is enum value type. I am trying to convert that to string value so it is easier to read when it is returned in output.

Below is my code that gets the data from sql server database.

ENUM

namespace CoreLibrary.Models.Enums
{
    public static class Common
    {
        public enum Status
        {


    Active = 1,
        InActive,
        Completed,
        Failed,
        InProgress,
        Pause,
        Resume,
        Skip,
        Running
    }
}

}

Entity Model:

namespace CoreLibrary.Models.Entities
{
    public class Deployments : BaseProperties
    {
        public int EnterpriseServiceId { get; set; }
        [ForeignKey("EnterpriseServiceId")]
        public virtual EnterpriseServices EnterpriseServices { get; set; }
        public Common.Status Status { get; set; }
    }
}

Method that retrieves data from database:

[HttpGet("~/api/Deployments/GetWithJoins")]
public JsonResult GetWithJoins()
{
    try
    {
        // includeList for including data from external tables (JOIN Query)
        var includeList = new List<Expression<Func<Deployments, object>>>();
        includeList.Add(d => d.EnterpriseServices);

        IEnumerable<Deployments> result = _repository.GetByIdWithJoins(queryable: includeList).ToList();
        return Json(result);
    }
    catch (Exception ex)
    {
        var innerException = ex.InnerException == null ? null : ex.InnerException.Message.ToString();
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new { status = "failed", message = ex.Message.ToString(), innerException = innerException });
    }
}

The result variable returns the output I shared at very beginning of this post. I am trying to convert status to return string value instead of int.

I am not sure if thats even possible or it is the right way to output of status int to string value?

I tried online, but I havent found a solution to my requirement. I would appreciate any help you guys can give. :)

Ray
  • 1,095
  • 3
  • 18
  • 43
  • 1
    You could simply cast an int to an enum? As already discussed here: https://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp – Tony Thomas Dec 30 '17 at 01:31
  • hmm that one is more of a single int to string. How do I convert array of object without iterating over each object to update `status`? – Ray Dec 30 '17 at 01:39
  • Is your class Deployments internally generated? – Tony Thomas Dec 30 '17 at 02:26
  • well its a class that I created for model. I hope that answers the question? – Ray Dec 30 '17 at 02:29
  • 1
    You could simply have a string property: `public string StatusText { get { return Status.ToString(); } }` in that class – Tony Thomas Dec 30 '17 at 02:59
  • Thanks Tony. That's also a good suggestion. – Ray Dec 30 '17 at 03:06
  • I removed my duplicate flag because I think I pointed at the wrong post. It should actually be duplicate of https://stackoverflow.com/questions/2441290/json-serialization-of-enum-as-string – Tony Thomas Dec 30 '17 at 03:20

1 Answers1

2

When we call JSON(someObject), ASP.NET uses its JavaScriptSerializer to create a JsonResult. The default configuration of the JavaScriptSerializer converts enums to their integer representation.

We can configure the serializer to use a string representation of enums instead. There are details on how to do that here: JSON serialization of enum as string

If that approach does not appeal, then you can use LINQ to map to an anonymous object using Enum.ToString(). Here is an example:

var result = deployments.Select(x =>
{
    return new 
    {
        Status = x.Status.ToString(),
        x.EnterpriseServiceId
    };
});

While the LINQ approach will work, it might lead to maintenance problems in the long-term.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    first approach worked very well. I was able to retrieve json with string value for `status`. Also thanks for the alternative option. – Ray Dec 30 '17 at 03:02