This project has moved. For the latest updates, please go here.

NullVallHandling Broken?

May 26, 2010 at 9:58 PM

Hello.  I am sending a JSON string built on the client and sending to the server for deserialization into an object.  I am trying to figure out how to get properties with either a value of null or a value of "" to be ignored.

So for dates, I have a class that has the following property on the server:

        DateTime _DateOfBirth;
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        [DataMapping("DateOfBirth", "")]  // <---- used by another 3rd party mapper
        public DateTime DateOfBirth



When I use the following JSON.Net code to deserialize the object, it not retruns an error when trying to convert this field from null to DataTime, and it should be ignoring it altogether.

                BO.UserProfile oUserProfile = JsonConvert.DeserializeObject<BO.UserProfile>(context.Request["myJsonwithDOB"]);

This exact code used to work with a previous version of 3.5 release...but can't tell which one as they are not marked for releases within 3.5.  Am I missing somthing?  Is there a better way to deal with null values that this?

Also, how can I set a default value that will be used if no value is specified in the JSON string?  I would love to see an example of marking a class property with the DefaultValue atttibute as I cannot find any examples and have tried to tag a property many different ways to no avail.




May 27, 2010 at 8:45 AM

NullValueHandling should now work more like expected with the change I just checked into source control.

To handle empty strings add "DefaultValueHandling = DefaultValueHandling.Ignore" to the JsonProperty and add a DefaultValue("") attribute.

May 27, 2010 at 2:16 PM
Edited May 27, 2010 at 4:51 PM

Thanks James...looks like values are now now properly set to null.  I opted to go the code route rather then the property attribute route as that way you do not have to go mark all your class properties if you have a ton of classes:


                // Setup JSON.Net Serializer object properties
                Newtonsoft.Json.JsonSerializerSettings jsonSettings = new Newtonsoft.Json.JsonSerializerSettings();
                jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;

                BO.UserProfile oUserProfile = JsonConvert.DeserializeObject(context.Request["profile"], jsonSettings);


However, I am still unable to mark a property with the DefaultValue("") attribute.  Using VS 2010 with ref to Newtonsoft.Json.dll.  Have following using statements in class:


using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;


I have tried to mark a property like so:


        DateTime? _DateOfBirth;
        [DataMapping("DateOfBirth", "")]
        public DateTime? DateOfBirth


but it doesn't like it....immediately throws errors about the Type or Namespace not being found.  Am I missing a ref, or is this not attribute?  Have not used that attribute before so it could be a .Net one and I am missing the ref to the assembly it is included in.

Thanks for you timely responsiveness James, that is good stuff!


UPDATE:  Ok, I got it....System.ComponentModel.  Thanks again James for your diligent work and great response times to the community!