In JsonUtil, support DateTimeOffset. Fixes #218.
This commit is contained in:
parent
e0481e1276
commit
4202a6c9e1
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<string> { "Comte de la Fère", "Armand" }
|
||||
Nicknames = new List<string> { "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<Person>(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<string> Nicknames { get; set; }
|
||||
public DateTimeOffset BirthInstant { get; set; }
|
||||
}
|
||||
|
||||
enum Hobbies { Reading = 1, Swordfighting = 2 }
|
||||
|
|
|
|||
Loading…
Reference in New Issue