Can't make MVC4 WebApi include null fields in

2019-04-29 03:35发布

I'm trying to serialize objects as JSON with MVC4 WebAPI (RTM - just installed VS2012 RTM today but was having this problem yesterday in the RC) and I'd like for all nulls to be rendered in the JSON output. Like this:

[{"Id": 1, "PropertyThatMightBeNull": null},{"Id":2, "PropertyThatMightBeNull": null}]

But what Im getting is

[{"Id":1},{"Id":2}]

I've found this Q/A WebApi doesnt serialize null fields but the answer either doesn't work for me or I'm failing to grasp where to put the answer.

Here's what I've tried:

  1. In Global.asax.cs's Application_Start, I added:

    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
    json.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include;
    

    This doesn't (seem to) error and seems to actually execute based on looking at the next thing I tried.

  2. In a controller method (in a subclass of ApiController), added:

    base.Configuration.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
    base.Configuration.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include;
    

    I say #1 executed because both values in #2 were already set before those lines ran as I stepped through.

  3. In a desperation move (because I REALLY don't want to decorate every property of every object) I tried adding this attrib to a property that was null and absent:

    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include,
        NullValueHandling = NullValueHandling.Include)]
    

All three produce the same JSON with null properties omitted.

Additional notes:

  • Running locally in IIS (tried built in too), Windows 7, VS2012 RTM.
  • Controller methods return List -- tried IEnumerable too
  • The objects I'm trying to serialize are pocos.

4条回答
▲ chillily
2楼-- · 2019-04-29 03:46

I finally came across this http://forums.asp.net/t/1824580.aspx/1?Serializing+to+JSON+Nullable+Date+gets+ommitted+using+Json+NET+and+Web+API+despite+specifying+NullValueHandling which describes what I was experiencing as a bug in the beta that was fixed for the RTM.

Though I had installed VS2012 RTM, my project was still using all the nuget packages that the beta came with. So I nugetted (nugot?) updates for everything and all is now well (using #1 from my question). Though I'm feeling silly for having burned half a day.

查看更多
爷、活的狠高调
3楼-- · 2019-04-29 03:49

For some odd reason the Newtonsoft.Json.JsonFormatter ignore assigments to the propreties os SerializerSettings.

In order to make your setting work create new instance of .SerializerSettings as shown below:

config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
        {

            DefaultValueHandling =   Newtonsoft.Json.DefaultValueHandling.Include,
            NullValueHandling = Newtonsoft.Json.NullValueHandling.Include,

        };
查看更多
beautiful°
4楼-- · 2019-04-29 03:51

When I saw this answer I was upset because I was already doing this and yet my problem still existed. My problem rooted back to the fact that my object implemented an interface that included a nullable type, so, I had a contract stating if you want to implement me you have to have one of these, and a serializer saying if one of those is null don't include it. BOOM!

查看更多
叛逆
5楼-- · 2019-04-29 04:05

This won't work:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;

But this does:

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() 
{ 
NullValueHandling = Newtonsoft.Json.NullValueHandling.Include
};
查看更多
登录 后发表回答