Home > Software design >  JSONSerializer deserialize to c# Object missing values
JSONSerializer deserialize to c# Object missing values

Time:07-06

I have JSON string containing many values and sub-values that I am trying to deserialize to a ServiceTicketRAF object. Thing is I don't know why it is setting null for three properties in the RafSpecialAccount class:

  1. specialAccountFinancialInstitutionAddress
  2. specialAccountOpenDate
  3. specialAccountClosedDate

Here's the method:

public string convertJSONToString(string json){

string payload = "";

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,                    
};

ServiceTicketRAF serviceTicketRAF =
    JsonSerializer.Deserialize<ServiceTicketRAF>(json, options)!;

And when the deserialize method is called, here's what the debugger returns for the ServiceTicketRAF object:

the debugger

Is there anything to do to make sure these keys/values pass through the deserializer correctly? Otherwise than the structural object, i'm not quite sure.

Here's the string json:

@"{
         ""notaryCode"": ""B0031"",
         ""rafYear"": ""2022"",
         ""rafProvider"": ""logicielEtude"",
         ""rafAccounts"": [
             {
                 ""institutionNumber"": ""006"",
                 ""branchNumber"": ""01234"",
                 ""accountNumber"": ""XXXXX"",
                 ""accountAmount"": ""1258.36""
             },
             {
                 ""institutionNumber"": ""815"",
                 ""branchNumber"": ""00123"",
                 ""accountNumber"": ""XXXXX"",
                 ""accountAmount"": ""2000.00""
             }
         ],
         ""rafAccountTotal"": ""3258.36"",
         ""rafSpecialAccounts"": [
             {
                 ""specialAccountFinancialInstitutionAddress "": ""123, rue des Test, Montréal, QC, J3J 3J3,(514) 123-1234"",
                 ""specialAccountOpenDate "": ""2022-02-10"",
                 ""specialAccountClosedDate "": ""2022-02-11"",
                 ""specialAccountClientNumber"": ""13e25"",
                 ""specialAccountNumber"": ""XXXXX"",
                 ""specialAccountFinancialInstitutionName"": ""Trust Général ABC"",
                 ""specialAccountAmount"": ""1000.23""
             },
             {
                 ""specialAccountFinancialInstitutionAddress "": ""456, rue Testing-123, Québec, QC, K3K 3K3, (418) 123-1234"",
                 ""specialAccountOpenDate "": ""2022-03-10"",
                 ""specialAccountClosedDate "": ""2022-03-11"",
                 ""specialAccountClientNumber"": ""11234e55"",
                 ""specialAccountNumber"": ""XXXXX"",
                 ""specialAccountFinancialInstitutionName"": ""Fiducie XYZ Inc."",
                 ""specialAccountAmount"": ""50000.06""
             }
         ],
         ""rafSpecialAccountTotal"": ""51000.29"",
         ""rafMonthlyBalance"": [
             {
                 ""month"": ""01"",
                 ""monthRevenues"": ""10.00"",
                 ""monthCharges"": ""10.00""
             },
             {
                 ""month"": ""02"",
                 ""monthRevenues"": ""20.00"",
                 ""monthCharges"": ""20.00""
             },
             {
                 ""month"": ""03"",
                 ""monthRevenues"": ""30.00"",
                 ""monthCharges"": ""30.00""
             },
             {
                 ""month"": ""04"",
                 ""monthRevenues"": ""40.00"",
                 ""monthCharges"": ""40.00""
             },
             {
                 ""month"": ""05"",
                 ""monthRevenues"": ""50.00"",
                 ""monthCharges"": ""50.00""
             },
             {
                 ""month"": ""06"",
                 ""monthRevenues"": ""60.00"",
                 ""monthCharges"": ""60.00""
             },
             {
                 ""month"": ""07"",
                 ""monthRevenues"": ""70.00"",
                 ""monthCharges"": ""70.00""
             },
             {
                 ""month"": ""08"",
                 ""monthRevenues"": ""80.00"",
                 ""monthCharges"": ""80.00""
             },
             {
                 ""month"": ""09"",
                 ""monthRevenues"": ""90.00"",
                 ""monthCharges"": ""90.00""
             },
             {
                 ""month"": ""10"",
                 ""monthRevenues"": ""100.00"",
                 ""monthCharges"": ""100.00""
             },
             {
                 ""month"": ""11"",
                 ""monthRevenues"": ""110.00"",
                 ""monthCharges"": ""110.00""
             },
             {
                 ""month"": ""12"",
                 ""monthRevenues"": ""120.00"",
                 ""monthCharges"": ""120.00""
             }
         ],
         ""previousBalance"": ""11.11"",
         ""totalRevenues"": ""780.00"",
         ""totalAnnualRevenues"": ""791.11"",
         ""totalCharges"": ""780.00"",
         ""totalBalance31dec"": ""00.00"",
         ""totalAnnualCharges"": ""780.00"",
         ""rafAmountDue"": [
             {
                 ""amountDueClientNumber"": ""abc123"",
                 ""amountDueLastDate"": ""2022-02-10"",
                 ""amountDue"": ""100.00"",
                 ""amountDueExplanation"": ""Lorem ipsum volutpat nulla. Nam vestibulum vitae arcu et facilisis. 123,88$ (-5.99%)""
             },
             {
                 ""amountDueClientNumber"": ""xyz321"",
                 ""amountDueLastDate"": ""2022-02-10"",
                 ""amountDue"": ""100.00"",
                 ""amountDueExplanation"": """"
             }
         ],
         ""rafTotalAmountDue"": ""200.00"",
         ""rafCheque"": [
             {
                 ""chequeClientNumber"": ""qwerty1"",
                 ""chequeNumber"": ""2568"",
                 ""chequeDate"": ""2022-02-10"",
                 ""chequeAmount"": ""100.00"",
                 ""chequeExplanation"": ""Lorem ipsum volutpat nulla. Nam vestibulum vitae arcu et facilisis.123,88$ (-5.99%)""
             },
             {
                 ""chequeClientNumber"": ""qwerty2"",
                 ""chequeNumber"": ""2168"",
                 ""chequeDate"": ""2022-02-11"",
                 ""chequeAmount"": ""200.00"",
                 ""chequeExplanation"": """"
             }
         ],
         ""rafTotalChequeAmount"": ""300.00"",
         ""rafRevenue"": [
             {
                 ""revenueClientNumber"": ""qwerty3"",
                 ""revenueReceptionDate"": ""2022-02-10"",
                 ""revenueDepositDate"": ""2022-02-11"",
                 ""revenueAmount"": ""100.00""
             },
             {
                 ""revenueClientNumber"": ""qwerty4"",
                 ""revenueReceptionDate"": ""2022-03-10"",
                 ""revenueDepositDate"": ""2022-03-11"",
                 ""revenueAmount"": ""200.00""
             }
         ],
         ""rafTotalRevenueAmount"": ""300.00"",
         ""rafGood"": [
             {
                 ""goodClientNumber"": ""qwerty5"",
                 ""goodDescription"": ""Nam éééligula ante, auctor ac lectus non, tincidunt suscipit lorem. Ut condimentum pharetra orci et facilisis. Phasellus at sapien vestibulum, gravida augue eget, volutpat nulla. Nam vestibulum vitae arcu et facilisis. 123,88$ (-5.99%)"",
                 ""goodDeliveryDate"": ""2022-02-10"",
                 ""goodRemittanceDate"": ""2022-02-11"",
                 ""goodExplanation"": ""Lorem ipsum volutpat nulla. Nam vestibulum vitae arcu et facilisis. 123,88$ (-5.99%)""
             },
             {
                 ""goodClientNumber"": ""qwerty6"",
                 ""goodDescription"": ""test1"",
                 ""goodDeliveryDate"": ""2022-03-10"",
                 ""goodRemittanceDate"": ""2022-03-11"",
                 ""goodExplanation"": """"
             }
         ]
     }";

here's the structure:

 public class ServiceTicketRAF
        {
            public const string categoryCode = "RAF";
            public const string subjectCode = "2021";
            public const string topicCode = "2021";
            public const string originCode = "01";

            public string notaryCode { get; set; }
            public string rafYear { get; set; }
            public string rafProvider { get; set; }
            public IList<RafAccount> rafAccounts { get; set; } //<Dictionary<string,RafAccount>>
            public string rafAccountTotal { get; set; }
            public IList<RafSpecialAccount> rafSpecialAccounts { get; set; }
            public string rafSpecialAccountTotal { get; set; }
            public IList<RafMonthlyBalance> rafMonthlyBalance { get; set; }
            public string previousBalance { get; set; }
            public string totalRevenues { get; set; }
            public string totalAnnualRevenues { get; set; }
            public string totalCharges { get; set; }
            public string totalBalance31dec { get; set; }
            public string totalAnnualCharges { get; set; }
            public IList<RafAmountDue> rafAmountDue { get; set; }
            public string rafTotalAmountDue { get; set; }
            public IList<RafCheque> rafCheque { get; set; }
            public string rafTotalChequeAmount { get; set; }
            public IList<RafRevenue> rafRevenue { get; set; }
            public string rafTotalRevenueAmount { get; set; }
            public IList<RafGood> rafGood { get; set; }
        }

        public class RafAccount
        {

            public string institutionNumber { get; set; }
            public string branchNumber { get; set; }
            public string accountNumber { get; set; }
            public string accountAmount { get; set; }
        }

        public class RafSpecialAccount
        {

            public string specialAccountClientNumber { get; set; }
            public string specialAccountNumber { get; set; }
            public string specialAccountFinancialInstitutionName { get; set; }
            public string specialAccountFinancialInstitutionAddress { get; set; }
            public string specialAccountOpenDate { get; set; }
            public string specialAccountClosedDate { get; set; }
            public string specialAccountAmount { get; set; }
        }

        public class RafMonthlyBalance
        {

            public string month { get; set; }
            public string monthRevenues { get; set; }
            public string monthCharges { get; set; }
        }

        public class RafAmountDue
        {

            public string amountDueClientNumber { get; set; }
            public string amountDueLastDate { get; set; }
            public string amountDue { get; set; }
            public string amountDueExplanation { get; set; }
        }

        public class RafCheque
        {

            public string chequeClientNumber { get; set; }
            public string chequeNumber { get; set; }
            public string chequeDate { get; set; }
            public string chequeAmount { get; set; }
            public string chequeExplanation { get; set; }
        }

        public class RafRevenue
        {

            public string revenueClientNumber { get; set; }
            public string revenueReceptionDate { get; set; }
            public string revenueDepositDate { get; set; }
            public string revenueAmount { get; set; }
        }

        public class RafGood
        {

            public string goodClientNumber { get; set; }
            public string goodDescription { get; set; }
            public string goodDeliveryDate { get; set; }
            public string goodRemittanceDate { get; set; }
            public string goodExplanation { get; set; }
        }

CodePudding user response:

I reproduced your issue and it would seem that the json string as posted has trailing white space in the property names for the three cases you point out.

Two occurrences of:

""specialAccountFinancialInstitutionAddress "" // Should be
""specialAccountFinancialInstitutionAddress""

Two occurrences of:

""specialAccountOpenDate "" // Should be
""specialAccountOpenDate""

Two occurrences of:

""specialAccountClosedDate "" // Should be
""specialAccountClosedDate""

When the trailing white space of the property names is removed so that the property can now find a match, there seems to be no other issue deserializing it to your class.

enter image description here

  • Related