Fix losing DateTime.Kind when deserializing ISO date strings (#1727)
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
#region License
|
||||||
|
// Copyright (c) 2007 James Newton-King
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person
|
||||||
|
// obtaining a copy of this software and associated documentation
|
||||||
|
// files (the "Software"), to deal in the Software without
|
||||||
|
// restriction, including without limitation the rights to use,
|
||||||
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the
|
||||||
|
// Software is furnished to do so, subject to the following
|
||||||
|
// conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be
|
||||||
|
// included in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using Newtonsoft.Json.Utilities;
|
||||||
|
#if DNXCORE50
|
||||||
|
using Xunit;
|
||||||
|
using Test = Xunit.FactAttribute;
|
||||||
|
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||||
|
#else
|
||||||
|
using NUnit.Framework;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace Newtonsoft.Json.Tests.Issues
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class Issue1708 : TestFixtureBase
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Test_DateTime()
|
||||||
|
{
|
||||||
|
JsonTextReader jsonTextReader = new JsonTextReader(new StringReader("'2018-05-27T23:25:08Z'"));
|
||||||
|
jsonTextReader.DateParseHandling = DateParseHandling.None;
|
||||||
|
jsonTextReader.Read();
|
||||||
|
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
DateTime dt = serializer.Deserialize<DateTime>(jsonTextReader);
|
||||||
|
|
||||||
|
Assert.AreEqual(DateTimeKind.Utc, dt.Kind);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -953,15 +953,23 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
if (contract.IsEnum)
|
if (contract.IsEnum)
|
||||||
{
|
{
|
||||||
if (value is string)
|
if (value is string s)
|
||||||
{
|
{
|
||||||
return EnumUtils.ParseEnum(contract.NonNullableUnderlyingType, value.ToString(), false);
|
return EnumUtils.ParseEnum(contract.NonNullableUnderlyingType, s, false);
|
||||||
}
|
}
|
||||||
if (ConvertUtils.IsInteger(primitiveContract.TypeCode))
|
if (ConvertUtils.IsInteger(primitiveContract.TypeCode))
|
||||||
{
|
{
|
||||||
return Enum.ToObject(contract.NonNullableUnderlyingType, value);
|
return Enum.ToObject(contract.NonNullableUnderlyingType, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (contract.NonNullableUnderlyingType == typeof(DateTime))
|
||||||
|
{
|
||||||
|
// use DateTimeUtils because Convert.ChangeType does not set DateTime.Kind correctly
|
||||||
|
if (value is string s && DateTimeUtils.TryParseDateTime(s, reader.DateTimeZoneHandling, reader.DateFormatString, reader.Culture, out DateTime dt))
|
||||||
|
{
|
||||||
|
return DateTimeUtils.EnsureDateTime(dt, reader.DateTimeZoneHandling);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if HAVE_BIG_INTEGER
|
#if HAVE_BIG_INTEGER
|
||||||
if (value is BigInteger integer)
|
if (value is BigInteger integer)
|
||||||
|
|||||||
Reference in New Issue
Block a user