From 64d7091b2b213ab462b65eba809ceefb9cb53376 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 28 Mar 2018 06:28:41 -0400 Subject: [PATCH] fix #396 - added TimeSpan serialization and deserialization --- .../Json/SimpleJson/SimpleJson.cs | 4 ++++ test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs b/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs index f30d3104d9..ff7cf93041 100644 --- a/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs +++ b/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs @@ -1349,6 +1349,8 @@ namespace SimpleJson { if (str.Length != 0) // We know it can't be null now. { + if (type == typeof(TimeSpan) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(TimeSpan))) + return TimeSpan.ParseExact(str, "c", CultureInfo.InvariantCulture); if (type == typeof(DateTime) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime))) return DateTime.TryParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var result) ? result : DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); @@ -1490,6 +1492,8 @@ namespace SimpleJson output = ((Guid)input).ToString("D"); else if (input is Uri) output = input.ToString(); + else if (input is TimeSpan) + output = ((TimeSpan)input).ToString("c"); else { Enum inputEnum = input as Enum; diff --git a/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs b/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs index 5f2e883cb6..b558244fea 100644 --- a/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs @@ -51,12 +51,13 @@ namespace Microsoft.AspNetCore.Blazor.Test Pets = new[] { "Aramis", "Porthos", "D'Artagnan" }, Hobby = Hobbies.Swordfighting, Nicknames = new List { "Comte de la Fère", "Armand" }, - BirthInstant = new DateTimeOffset(1825, 8, 6, 18, 45, 21, TimeSpan.FromHours(-6)) + BirthInstant = new DateTimeOffset(1825, 8, 6, 18, 45, 21, TimeSpan.FromHours(-6)), + Age = new TimeSpan(7665, 1, 30, 0) }; // Act/Assert Assert.Equal( - "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"],\"BirthInstant\":\"1825-08-06T18:45:21.0000000-06:00\"}", + "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"],\"BirthInstant\":\"1825-08-06T18:45:21.0000000-06:00\",\"Age\":\"7665.01:30:00\"}", JsonUtil.Serialize(person)); } @@ -64,7 +65,7 @@ namespace Microsoft.AspNetCore.Blazor.Test public void CanDeserializeClassFromJson() { // Arrange - var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"],\"BirthInstant\":\"1825-08-06T18:45:21.0000000-06:00\"}"; + var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"],\"BirthInstant\":\"1825-08-06T18:45:21.0000000-06:00\",\"Age\":\"7665.01:30:00\"}"; // Act var person = JsonUtil.Deserialize(json); @@ -76,6 +77,7 @@ namespace Microsoft.AspNetCore.Blazor.Test Assert.Equal(Hobbies.Swordfighting, person.Hobby); Assert.Equal(new[] { "Comte de la Fère", "Armand" }, person.Nicknames); Assert.Equal(new DateTimeOffset(1825, 8, 6, 18, 45, 21, TimeSpan.FromHours(-6)), person.BirthInstant); + Assert.Equal(new TimeSpan(7665, 1, 30, 0), person.Age); } class Person @@ -86,6 +88,7 @@ namespace Microsoft.AspNetCore.Blazor.Test public Hobbies Hobby { get; set; } public IList Nicknames { get; set; } public DateTimeOffset BirthInstant { get; set; } + public TimeSpan Age { get; set; } } enum Hobbies { Reading = 1, Swordfighting = 2 }