Fix losing DateTime.Kind when deserializing ISO date strings (#1727)

This commit is contained in:
James Newton-King
2018-05-28 20:54:01 +12:00
committed by GitHub
parent 3c11adaada
commit 42ee730f3a
2 changed files with 73 additions and 2 deletions
@@ -953,15 +953,23 @@ namespace Newtonsoft.Json.Serialization
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))
{
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 (value is BigInteger integer)