fix #396 - added TimeSpan serialization and deserialization

This commit is contained in:
Benjamin 2018-03-28 06:28:41 -04:00 committed by Steve Sanderson
parent 9a01ec5743
commit 64d7091b2b
2 changed files with 10 additions and 3 deletions

View File

@ -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;

View File

@ -51,12 +51,13 @@ namespace Microsoft.AspNetCore.Blazor.Test
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" },
Hobby = Hobbies.Swordfighting,
Nicknames = new List<string> { "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<Person>(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<string> Nicknames { get; set; }
public DateTimeOffset BirthInstant { get; set; }
public TimeSpan Age { get; set; }
}
enum Hobbies { Reading = 1, Swordfighting = 2 }