Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map JSON to C# Class with Attribute names having space and Reserve words

I am consuming a REST API service and in the Service output of the JSON's attribute names either consist of spaces and reserve words. Problem comes when I tried to map it to a C# Class but I cant assign attribute names similar to this.

Let me know if I can resolve this or is there any other alternative way.

Below is the JSON returned from the Service:

{
   "value": [   {
      "accountNo": "JKBXXXXXXXXXVNX1",
      "mainSalesPerson": "XXXXX",
      "accountName": "XXXXXX",
      "ledgerBalance": "-2,298.70",
      "T+3": "0.00",
      "T+4": "0.00",
      "T+5 and Above": "2,298.70",
      "riskRatedPortfolioValue": "109,057.50",
      "exposure": "106,758.80",
      "costToFirm": "",
      "incomeToFirm": "14.59",
      "costToIncome(%)": "",
      "exposure(%)": "2.11",
      "O/S Balance": "-2,298.70"
   }],
   "success": true
}

I have used the following Mapping C# Class:

public class CountObject
    {
        public string value { get; set; }
        public string success { get; set; }
    }

    public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [SerializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [SerializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [SerializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [SerializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [SerializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [SerializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }

    public class RiskManagementQueryHeadertObject
    {
        public List<RiskManagementQueryValueObject> value { get; set; }
        public bool success { get; set; }
    }

Pls note that I have used the above, but the mapping exactly happens when the JSON attribute and C# Class name matches, rest of the value is null

And this How I talk to the Service:

public List<RiskManagementQueryValueObject> getOverdues()
        {
            List<RiskManagementQueryValueObject> overdues = new List<RiskManagementQueryValueObject>();
            var count = 0.0;

            try
            {
                var restProxy = new RESTProxy();
                var request = restProxy.initRestRequest("/cam/riskmanagement/1/query/count", Method.POST, new { requestCriteriaMap = new { currency = "LKR" } });
                CountObject data = restProxy.Execute<CountObject>(request);

                if (data.success.ToString().ToLower() != "true")
                    return null;

                //get the page count
                var pageCount = Math.Truncate(count / 300) + 1;

                for (int j = 0; j < pageCount; j++)
                {
                    var request2 = restProxy.initRestRequest(string.Format("/cam/riskmanagement/1/query/{0}", (j + 1).ToString()), Method.POST, new { requestCriteriaMap = new { currency = "LKR" } });
                    RiskManagementQueryHeadertObject data2 = restProxy.Execute<RiskManagementQueryHeadertObject>(request2);

                    overdues.AddRange(data2.value);
                }
            }
            catch (Exception)
            {
                overdues = null;
            }

            return overdues;
        }
    }

    public class RESTProxy
        {
            string DEFAULT_HOST = "http://xxx.xxx.xxx.xxx:xxx/";
            string DEFAULT_MODULE = "xxx-rest";
            string DEFAULT_USER = "xxx:xxx:xxx";
            string DEFAULT_PASS = "xxx";

            public T Execute<T>(RestRequest request) where T : new()
            {
                var client = new RestClient(string.Concat(DEFAULT_HOST, DEFAULT_MODULE))
                {
                    Authenticator = new DigestAuthenticator(DEFAULT_USER, DEFAULT_PASS, DEFAULT_HOST)
                };

                var response = client.Execute<T>(request);

                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }
                return response.Data;
            }

            public RestRequest initRestRequest(string pRestResourceName, Method pRestServiceVerb, object pRestRequestBody)
            {
                var request = new RestRequest(pRestResourceName, pRestServiceVerb);
                request.RequestFormat = DataFormat.Json;


                if (pRestRequestBody != null)
                {
                    request.AddBody(pRestRequestBody);
                }

                return request;
            }
        }

How can I resolve such thing?

Update

I solved the issue. I added [DeserializeAs(Name ="")] instead of [SerializeAs(Name ="")] I just did this in my C# Map class:

public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [DeserializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [DeserializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [DeserializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [DeserializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [DeserializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [DeserializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }
like image 647
hiFI Avatar asked Jul 15 '16 08:07

hiFI


2 Answers

Using the JsonProperty attribute worked for me:

Example:

This C# class:

public class Product
{
    [JsonProperty(PropertyName = "Name/ + 1")]
    public string Name { get; set; }
}

maps to the following javascript object:

var input = {"Name/ + 1": "PostName"};
like image 108
poppertech Avatar answered Sep 26 '22 13:09

poppertech


I solved the issue. I added [DeserializeAs(Name ="")] instead of [SerializeAs(Name ="")]. I just did this in my C# Map class:

public class RiskManagementQueryValueObject
    {
        public string accountNo { get; set; }
        public string mainSalesPerson { get; set; }
        public string accountName { get; set; }
        public string ledgerBalance { get; set; }

        [DeserializeAs(Name = "T+3")]
        public string T3 { get; set; }

        [DeserializeAs(Name = "T+4")]
        public string T4 { get; set; }

        [DeserializeAs(Name = "T+5 and Above")]
        public string T5_and_Above { get; set; }
        public string riskRatedPortfolioValue { get; set; }
        public string exposure { get; set; }
        public string costToFirm { get; set; }
        public string incomeToFirm { get; set; }

        [DeserializeAs(Name = "costToIncome(%)")]
        public string costToIncome { get; set; }

        [DeserializeAs(Name = "exposure(%)")]
        public string exposure_per { get; set; }

        [DeserializeAs(Name = "O/S Balance")]
        public string OS_Balance { get; set; }
    }
like image 35
hiFI Avatar answered Sep 23 '22 13:09

hiFI