Json Serialization Issue

Discussions relating to the REST API of Jiwa 7.

Json Serialization Issue

Postby SBarnes » Fri Mar 13, 2020 10:53 am

I have an issue where, from the following code when the API produces Json any property with a zero value such as Price does not appear on the object, if XML or csv are given then the price shows up regardless of its value.

How can I resolve this?

Ignore the Formatted function that is for something else

Code: Select all
 [Serializable]   
   [DataContract]
      public class DebtorOPEAFileExportResponse
      {
         [DataMember]
         public string CompanyID {get; set;}
         [DataMember]
         public DateTime EffectiveDate {get; set;}         
         [DataMember]
         public string PartNo {get; set;}
         [DataMember]
           public string Description {get; set;}
         [DataMember]
         public double Price {get; set;}
         [DataMember]
         public bool GST {get; set;}
         [DataMember]
         public double RRPrice {get; set;}
         [DataMember]
         public string DiscountCode {get; set;}         
         [DataMember]
         public string SupersededPartNo {get; set;}         
         [DataMember]
         public bool Active {get; set;}

         
         [DataMember]
         public string StockingCode {get; set;}   
         
         
         [DataMember]
         public double MOQ {get; set;}   
         
         [DataMember]
         public string Class {get; set;}   
         
         [DataMember]
         public string Filler {get; set;}   
         
         public DebtorOPEAFileExportResponse()
         {
         
            CompanyID  = "";
            
            EffectiveDate  = DateTime.Now;         
            
            PartNo  = "";
            
              Description  = "";
            
            Price  = 0;
            
            GST  = false;
            
            RRPrice  = 0;
            
            DiscountCode  = "";         
            
            SupersededPartNo  = "";         
            
            Active  = false;
            
               
            
            StockingCode  = "";   
            
            
            
            MOQ  = 0;   
            
            
            Class  = "";   
            
            
            Filler  = "";               
         }
         
         public string Formated()
         {
            string result = "";
            
            result += CompanyID.OPEAPadRight(4);
            result += EffectiveDate.ToString("yyMMdd").OPEAPadRight(6);
            result += PartNo.OPEAPadRight(20);
            result += Description.OPEAPadRight(35);
            result += (Price *100).OPEARoundedString(0).OPEAPadLeft(10, '0');
            result += (RRPrice *100).OPEARoundedString(0).OPEAPadLeft(10, '0');
            result += DiscountCode.OPEAPadRight(2);
            result += SupersededPartNo.OPEAPadRight(20);
            result += Active ? "A" : "N";
            result += GST ? "1" : "0";
            result += StockingCode.OPEAPadRight(1);
            result += MOQ.OPEARoundedString(0).OPEAPadRight(4);
            result += Class.OPEAPadRight(4);
            result += Filler.OPEAPadRight(8);
            
            
            return result;
         }         
         
      }
      
   public static class ExtHelper
   {
      
      public static string  OPEAPadLeft(this string str, int TotalWidth, char PadChar = ' ')
      {
         return str.PadLeft(TotalWidth, PadChar).Substring(0, TotalWidth);
      }
      
      public static string  OPEAPadRight(this string str, int TotalWidth, char PadChar = ' ')
      {
         string strPadded = str.PadRight(TotalWidth, PadChar);
         return strPadded.Substring(strPadded.Length - TotalWidth, TotalWidth);
      }
      
      public static string  OPEARoundedString(this double val, int decimals)
      {
         
         return Math.Round(val, decimals).ToString();
      }      
      
   }
            
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1618
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Json Serialization Issue

Postby Mike.Sheen » Sat Mar 14, 2020 12:01 pm

SBarnes wrote:I have an issue where, from the following code when the API produces Json any property with a zero value such as Price does not appear on the object, if XML or csv are given then the price shows up regardless of its value.

How can I resolve this?


In our configure method of our RESTAPIPlugin class, we have the JsConfig.ExcludeDefaultValues property set to true:

Code: Select all
//When using the API, null or default values should be excluded
ServiceStack.Text.JsConfig.ExcludeDefaultValues = true;


I believe you want to set that to false.

If I recall correctly, we set this to true to simplify the json responses to only show values which are set (non-default) to make it easier to read. It shouldn't do any harm to turn it on, it'll just make the json responses more noisy.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Json Serialization Issue

Postby Mike.Sheen » Sat Mar 14, 2020 12:04 pm

I found the original issue calling for this to be turned on - DEV-6164
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Json Serialization Issue

Postby SBarnes » Sat Mar 14, 2020 12:24 pm

Hi Mike

I don't know much about ServiceStack.Text does it support something like Newtonsoft does where you can inherit from JsonConverter, as I am a little cautious about resetting something on the overall api given we are talking about something with not only the standard api but also a large customisation extension to the api?

See

https://www.newtonsoft.com/json/help/ht ... verter.htm
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1618
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Json Serialization Issue

Postby Mike.Sheen » Sat Mar 14, 2020 12:47 pm

SBarnes wrote:Hi Mike

I don't know much about ServiceStack.Text does it support something like Newtonsoft does where you can inherit from JsonConverter, as I am a little cautious about resetting something on the overall api given we are talking about something with not only the standard api but also a large customisation extension to the api?

See

https://www.newtonsoft.com/json/help/ht ... verter.htm


Hi Stuart,

You can customise the json responses - even per service, so your custom services can override the behaviour - see Customize JSON Responses.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Json Serialization Issue

Postby SBarnes » Sat Mar 14, 2020 3:28 pm

Ok thanks,

I'll take a look.

I also found this which may be another way of overcoming it https://forums.servicestack.net/t/how-t ... ack/5152/2
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1618
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to REST API

Who is online

Users browsing this forum: No registered users and 1 guest

cron