konrad konrad - 3 years ago 134
JSON Question

how to handle a null value in JSON data member for DateTime

I am receiving a JSON file from my service that looks like this:

{
"body": {
"assignedTo": "aa",
"message": "aaa",
"submittedOn": 1506703098946,
"completedOn": null,
"submittedBy": "web-user",
"completedBy": null,
"isSelected": false,
"name": "aaa"
}
}


The value of 'completedOn' is defined as a
DateTime
in my class but what I am getting from the server is null. Is there a way to handle that?

Here's my class structure:

[DataContract]
public class FamilyTask
{
private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[DataMember(Name = "Id")]
public string Id { get; set; }

[DataMember(Name = "name")]
public string name { get; set; }

[DataMember(Name = "assignedTo")]
public string assignedTo { get; set; }

[DataMember(Name = "message")]
public string message { get; set; }

[DataMember(Name = "submittedBy")]
public string submittedBy { get; set; }

[DataMember(Name = "completedBy")]
public string completedBy { get; set; }

[IgnoreDataMember]
public DateTime submittedOn { get; set; }

[DataMember(Name = "submittedOn")]
private long submittedOnTicks {
get { return (long) (submittedOn - unixEpoch).TotalMilliseconds; }
set { submittedOn = unixEpoch.AddMilliseconds(value); }
}

[IgnoreDataMember]
public DateTime completedOn { get; set; }

[DataMember(Name = "completedOn")]
private long completedOnTicks
{
get { return (long)(completedOn - unixEpoch).TotalMilliseconds; }
set { completedOn = unixEpoch.AddMilliseconds(value); }
}
}


EDIT:

I did try adding a nullable to the
public DateTime? completedOn{get;set;}
but that causes this issue:

enter image description here

enter image description here

Edit2:

I implemented the nullable class and a null check but I am still getting this:

enter image description here

Answer Source

Make it nullable by adding a questionmark like this:

public DateTime? CompletedOn { get; set;}

You can then check if it has a value by using .HasValue. The actual value can be accessed with .Value.

Regarding your edit, you need to use .Value here(you will need to check for null!)

private long completedOnTicks
{
    get
    { 
         if(completedOn.HasValue)
         {
              return (long)(completedOn.Value - unixEpoch).TotalMilliseconds; }
         }

         // completedOn is null, return something reasonable
         return long.MinValue;
    }
    set { completedOn = unixEpoch.AddMilliseconds(value); }
}

If the webservice returns a DateTime, Why are you not just doing it like this?

[DataMember(Name = "completedOn")]
public DateTime? CompletedOn { get; set;}

// completedOnTicks is not needed anymore?
private long completedOnTicks
{
        get
        { 
             if(CompletedOn.HasValue)
             {
                  return (long)(CompletedOn.Value - unixEpoch).TotalMilliseconds; }
             }

              // CompletedOn is null, return something reasonable
             return long.MinValue;
        }
}

If it does not return a DateTime but instead a long, just make your long nullable as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download