Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing to JSON, Nullable Date gets omitted using Json.NET and Web API despite specifying NullValueHandling

using Newtonsoft.Json;

namespace FAL.WebAPI2012.Controllers
{
    public class Person
    {
        public int Id {get;set;}
        public string FirstName {get;set;}
        public string LastName {get;set;}

        [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include, 
            NullValueHandling = NullValueHandling.Include)]
        public DateTime? Dob { get; set; }
    }

    public class TestNullsController : ApiController
    {
        // GET api/<controller>
        public Person Get()
        {
            Person myPerson = new Person() { 
                Dob = null, FirstName = "Adrian", Id=1, LastName="Bobby"
            };

            return myPerson;
        }
    }
}

As you can see, my Dob field is set to null but the result is the following

{ "Id":1, "FirstName":"Adrian", "LastName":"Bobby" }

and Dob is not serialized to null which I need it to be!

(I have tested that JsonProperty is setting other attributes like name and it changes the JSON output perfectly. I just can't get the nullable property to be serialized. Also, I have tested Json.Net (see answer below), so my thinking is that web api setup is overriding something somewhere, would be nice to know where).

like image 593
atreeon Avatar asked Jul 16 '12 18:07

atreeon


2 Answers

It would appear that the problem lies somewhere else. I've done the following in a console application:

using System;
using System.IO;
using Newtonsoft.Json;

namespace JsonNetNullablePropertyTest
{
    class Program
    {
        static void Main()
        {
            var myPerson = new Person {
                Dob = null,
                FirstName = "Adrian",
                Id = 1,
                LastName = "Bobby"
            };

            using (var textWriter = new StringWriter())
            using (var writer = new JsonTextWriter(textWriter))
            {
                // Create the serializer.
                var serializer = new JsonSerializer();

                // Serialize.
                serializer.Serialize(writer, myPerson);

                // Write the output.
                Console.WriteLine(textWriter);
            }
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include,
            NullValueHandling = NullValueHandling.Include)]
        public DateTime? Dob { get; set; }
    }
}

And the output is:

{"Id":1,"FirstName":"Adrian","LastName":"Bobby","Dob":null}

As expected.

It would seem that the problem lies in how you are actually calling the JsonSerializer to serialize the Person instance.

like image 90
casperOne Avatar answered Sep 28 '22 12:09

casperOne


I received this reply from a member of the web.api folk at Microsoft.

'You'll be glad to know this has been fixed since RC:

http://aspnetwebstack.codeplex.com/workitem/243

You can either upgrade to a newer package, or you can override our settings like this:

JsonFormatter.SerializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include };

Hope that helps'

So, all works with that update.

like image 25
atreeon Avatar answered Sep 28 '22 11:09

atreeon