WebAPI does not return JSON for INT when value is zero

I have this class as a clipped version:

public class SportTableRow { public Int32 Won { get; set; } public Int32 Lost { get; set; } public Int32 Drawn { get; set; } public Int32 For { get; set; } } 

When I make a data call via WebAPI, it looks like this (cut again) ...

 public List<SportTableRow> Get() { var options = .... var sport = .... var locationCode = ... return SportManager.GetOverallTable(sport, options, locationCode).TableRows; } 

When I check the returned data in the debugger, you can see the properties in the list ...

enter image description here

But, when I call through the violinist, you can see that some properties are missing ...

enter image description here

... and it seems like any Int that is 0, and bool that are false, etc.

Do I need to install something in the current class or something in the JSON serializer?

+6
source share
1 answer

The default JSON JSON.NET serializer throws an exception for properties that are set to default values. For example, boolean=false , int=0 , int?=null , object=null , etc. Will be excluded from received JSON. The goal is to minimize bandwidth.

You can change this behavior by changing the settings:

  System.Web.Http .GlobalConfiguration.Configuration .Formatters .JsonFormatter .SerializerSettings .DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include; 

It is best to add this line to the Global.asax file. But note: this will simply add bandwidth without real benefits, especially if you also control the client side.

+12
source

Source: https://habr.com/ru/post/957253/


All Articles