Skip to content

Commit 12106af

Browse files
Fix of deserializing Nullable datetime being null. Previously it returned DateTime.MinValue
1 parent 647bf5d commit 12106af

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/ServiceStack.Text/Common/DateTimeSerializer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public static class DateTimeSerializer
3131
public const string WcfJsonPrefix = "/Date(";
3232
public const char WcfJsonSuffix = ')';
3333

34-
public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
34+
public static DateTime? ParseShortestNullableXsdDateTime(string dateTimeStr)
35+
{
36+
if (dateTimeStr == null)
37+
return null;
38+
39+
return ParseShortestXsdDateTime(dateTimeStr);
40+
}
41+
42+
public static DateTime ParseShortestXsdDateTime(string dateTimeStr)
3543
{
3644
if (string.IsNullOrEmpty(dateTimeStr))
3745
return DateTime.MinValue;

src/ServiceStack.Text/Common/DeserializeBuiltin.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ private static ParseStringDelegate GetParseFn()
5252

5353
if (typeof(T) == typeof(Guid))
5454
return value => new Guid(value);
55-
if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTime?))
56-
return value => DateTimeSerializer.ParseShortestXsdDateTime(value);
55+
if (typeof(T) == typeof(DateTime?))
56+
return value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value);
57+
if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTime?))
58+
return value => DateTimeSerializer.ParseShortestXsdDateTime(value);
5759
if (typeof(T) == typeof(DateTimeOffset) || typeof(T) == typeof(DateTimeOffset?))
5860
return value => DateTimeSerializer.ParseDateTimeOffset(value);
5961
if (typeof(T) == typeof(TimeSpan))

tests/ServiceStack.Text.Tests/JsonTests/BasicJsonTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,61 @@ public static JsonPrimitives Create(int i)
3434
}
3535
}
3636

37+
public class NullableValueTypes
38+
{
39+
public int? Int { get; set; }
40+
public long? Long { get; set; }
41+
public decimal? Decimal { get; set; }
42+
public double? Double { get; set; }
43+
public bool? Boolean { get; set; }
44+
public DateTime? DateTime { get; set; }
45+
}
46+
47+
[Test]
48+
public void Can_parse_json_with_nullable_valuetypes()
49+
{
50+
var json = "{}";
51+
52+
var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
53+
54+
Assert.That(item.Int, Is.Null, "int");
55+
Assert.That(item.Long, Is.Null, "long");
56+
Assert.That(item.Decimal, Is.Null, "decimal");
57+
Assert.That(item.Double, Is.Null, "double");
58+
Assert.That(item.Boolean, Is.Null, "boolean");
59+
Assert.That(item.DateTime, Is.Null, "datetime");
60+
}
61+
62+
[Test]
63+
public void Can_parse_json_with_nullable_valuetypes_that_has_included_null_values()
64+
{
65+
var json = "{\"Int\":null,\"Long\":null,\"Decimal\":null,\"Double\":null,\"Boolean\":null,\"DateTime\":null}";
66+
67+
var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
68+
69+
Assert.That(item.Int, Is.Null, "int");
70+
Assert.That(item.Long, Is.Null, "long");
71+
Assert.That(item.Decimal, Is.Null, "decimal");
72+
Assert.That(item.Double, Is.Null, "double");
73+
Assert.That(item.Boolean, Is.Null, "boolean");
74+
Assert.That(item.DateTime, Is.Null, "datetime");
75+
}
76+
77+
[Test]
78+
public void Can_parse_json_with_nullable_valuetypes_that_has_no_value_specified()
79+
{
80+
var json = "{\"Int\":,\"Long\":,\"Decimal\":,\"Double\":,\"Boolean\":,\"DateTime\":}";
81+
82+
var item = JsonSerializer.DeserializeFromString<NullableValueTypes>(json);
83+
84+
Assert.That(item.Int, Is.Null, "int");
85+
Assert.That(item.Long, Is.Null, "long");
86+
Assert.That(item.Decimal, Is.Null, "decimal");
87+
Assert.That(item.Double, Is.Null, "double");
88+
Assert.That(item.Boolean, Is.Null, "boolean");
89+
Assert.That(item.DateTime, Is.Null, "datetime");
90+
}
91+
3792
[Test]
3893
public void Can_handle_json_primitives()
3994
{

0 commit comments

Comments
 (0)