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)
|
return DateTime.TryParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var result)
|
||||||
? result : DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
|
? result : DateTime.Parse(str, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
|
||||||
if (type == typeof(DateTimeOffset) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset)))
|
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)))
|
if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)))
|
||||||
return new Guid(str);
|
return new Guid(str);
|
||||||
if (type == typeof(Uri))
|
if (type == typeof(Uri))
|
||||||
|
|
@ -1484,7 +1485,7 @@ namespace SimpleJson
|
||||||
if (input is DateTime)
|
if (input is DateTime)
|
||||||
output = ((DateTime)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
|
output = ((DateTime)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
|
||||||
else if (input is DateTimeOffset)
|
else if (input is DateTimeOffset)
|
||||||
output = ((DateTimeOffset)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
|
output = ((DateTimeOffset)input).ToString("o");
|
||||||
else if (input is Guid)
|
else if (input is Guid)
|
||||||
output = ((Guid)input).ToString("D");
|
output = ((Guid)input).ToString("D");
|
||||||
else if (input is Uri)
|
else if (input is Uri)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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 System.Collections.Generic;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -49,12 +50,13 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
Name = "Athos",
|
Name = "Athos",
|
||||||
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" },
|
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" },
|
||||||
Hobby = Hobbies.Swordfighting,
|
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
|
// Act/Assert
|
||||||
Assert.Equal(
|
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));
|
JsonUtil.Serialize(person));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,7 +64,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
public void CanDeserializeClassFromJson()
|
public void CanDeserializeClassFromJson()
|
||||||
{
|
{
|
||||||
// Arrange
|
// 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
|
// Act
|
||||||
var person = JsonUtil.Deserialize<Person>(json);
|
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(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets);
|
||||||
Assert.Equal(Hobbies.Swordfighting, person.Hobby);
|
Assert.Equal(Hobbies.Swordfighting, person.Hobby);
|
||||||
Assert.Equal(new[] { "Comte de la Fère", "Armand" }, person.Nicknames);
|
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
|
class Person
|
||||||
|
|
@ -82,6 +85,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
public string[] Pets { get; set; }
|
public string[] Pets { get; set; }
|
||||||
public Hobbies Hobby { get; set; }
|
public Hobbies Hobby { get; set; }
|
||||||
public IList<string> Nicknames { get; set; }
|
public IList<string> Nicknames { get; set; }
|
||||||
|
public DateTimeOffset BirthInstant { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Hobbies { Reading = 1, Swordfighting = 2 }
|
enum Hobbies { Reading = 1, Swordfighting = 2 }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue