diff --git a/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs b/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs index 70f824da12..01075b22e8 100644 --- a/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs +++ b/src/Microsoft.AspNetCore.Blazor/Json/SimpleJson/SimpleJson.cs @@ -1353,7 +1353,8 @@ namespace SimpleJson return DateTime.TryParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var result) ? result : DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); if (type == typeof(DateTimeOffset) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset))) - return DateTimeOffset.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); + return DateTimeOffset.TryParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var result) + ? result : DateTimeOffset.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))) return new Guid(str); if (type == typeof(Uri)) @@ -1484,7 +1485,7 @@ namespace SimpleJson if (input is DateTime) output = ((DateTime)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture); else if (input is DateTimeOffset) - output = ((DateTimeOffset)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture); + output = ((DateTimeOffset)input).ToString("o"); else if (input is Guid) output = ((Guid)input).ToString("D"); else if (input is Uri) diff --git a/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs b/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs index 2f064d29e4..5f2e883cb6 100644 --- a/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Test/JsonUtilTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using Xunit; @@ -49,12 +50,13 @@ namespace Microsoft.AspNetCore.Blazor.Test Name = "Athos", Pets = new[] { "Aramis", "Porthos", "D'Artagnan" }, Hobby = Hobbies.Swordfighting, - Nicknames = new List { "Comte de la Fère", "Armand" } + Nicknames = new List { "Comte de la Fère", "Armand" }, + BirthInstant = new DateTimeOffset(1825, 8, 6, 18, 45, 21, TimeSpan.FromHours(-6)) }; // Act/Assert Assert.Equal( - "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"]}", + "{\"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\"}", JsonUtil.Serialize(person)); } @@ -62,7 +64,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\"]}"; + 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\"}"; // Act var person = JsonUtil.Deserialize(json); @@ -73,6 +75,7 @@ namespace Microsoft.AspNetCore.Blazor.Test Assert.Equal(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets); 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); } class Person @@ -82,6 +85,7 @@ namespace Microsoft.AspNetCore.Blazor.Test public string[] Pets { get; set; } public Hobbies Hobby { get; set; } public IList Nicknames { get; set; } + public DateTimeOffset BirthInstant { get; set; } } enum Hobbies { Reading = 1, Swordfighting = 2 }