Reputation: 3462
I have this problem where a null datetime in a list is not being serialized/deserialized properly.
see this sample code:
internal class WrapperNullableDateTimeList
{
public List<DateTime?> MyList { get; set; }
}
public void T14_Should_skip_output_nullable_datetime_in_list_TODO_THIS_IS_WRONG()
{
WrapperNullableDateTimeList input = new WrapperNullableDateTimeList();
input.MyList = new List<DateTime?>()
{
new DateTime(2000, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc),
null,
new DateTime(2000, 12, 31, 0, 0, 0, 0, DateTimeKind.Utc),
};
JsConfig.IncludeNullValues = true;
var serializer = new JsonSerializer<WrapperNullableDateTimeList>();
string serializedInput = serializer.SerializeToString(input);
WrapperNullableDateTimeList output = serializer.DeserializeFromString(serializedInput);
output.MyList.Count.Should().Be(3); // Fails here! The 'null' DateTime in the list is dropped
}
Any ideas? BTW, I printed the serializedInput (json), and it looks like this:
{"MyList":["2000-01-01T00:00:00.0000000Z",null,"2000-12-31T00:00:00.0000000Z"]}
I have my JsConfig to include null values... so what gives?
Upvotes: 0
Views: 135
Reputation: 143399
This works in the latest version of ServiceStack:
using (JsConfig.With(new Config { includeNullValues = true }))
{
var dto = new WrapperNullableDateTimeList
{
MyList = new List<DateTime?>
{
new DateTime(2000, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc),
null,
new DateTime(2000, 12, 31, 0, 0, 0, 0, DateTimeKind.Utc),
}
};
var json = dto.ToJson();
json.Print();
Assert.That(json, Is.EqualTo(
@"{""MyList"":[""\/Date(946684800000)\/"",null,""\/Date(978220800000)\/""]}"));
var fromJson = json.FromJson<WrapperNullableDateTimeList>();
Assert.That(fromJson.MyList.Count, Is.EqualTo(dto.MyList.Count));
}
Upvotes: 2