In JSON strings, empty date values aren't handled when deserializing to nullable DateTime fields

Aug 5, 2009 at 10:53 AM

Hi,

I have previously posted this question but saw that you have replied to the ones that was posted after that, but didn't answer mine. So I thought may be you've missed it and decided to post it again.

When I try to deserialize this data {Id: 1, Code: "A", BeginDate: ""} with JsonConvert.DeserializeObject by supplying type information, even though BeginDate is a nullable DateTime field I get an error.  I checked out the code and found that empty date string (dateText) is tried to be parsed:

if (!string.IsNullOrEmpty(_dateTimeFormat))
  return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
else
  return DateTime.Parse(dateText, Culture, _dateTimeStyles);

Shouldn't it check before it parses the dateText and return null if dateText is empty or null?

Thanks in advance

Coordinator
Aug 6, 2009 at 9:52 AM

Done. Source code checked in if you want to build it for yourself.

Aug 6, 2009 at 1:45 PM

Thanks