diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DateTimeZoneHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DateTimeZoneHandlingTests.cs new file mode 100644 index 00000000..49aae922 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Serialization/DateTimeZoneHandlingTests.cs @@ -0,0 +1,115 @@ +#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.Generic; +using System.Runtime.Serialization.Formatters; +using System.Text; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Tests.TestObjects; +using Newtonsoft.Json.Tests.TestObjects.Organization; +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.Serialization +{ + [TestFixture] + public class DateTimeZoneHandlingTests : TestFixtureBase + { + [Test] + public void DeserializeObject() + { + string json = @" + { + ""Value"": ""2017-12-05T21:59:00"" + }"; + + DateTimeWrapper c1 = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc + }); + + DateTimeWrapper c2 = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() + { + DateTimeZoneHandling = DateTimeZoneHandling.Local + }); + + DateTimeWrapper c3 = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() + { + DateTimeZoneHandling = DateTimeZoneHandling.Unspecified + }); + + DateTimeWrapper c4 = JsonConvert.DeserializeObject(json); + + Assert.AreEqual(DateTimeKind.Utc, c1.Value.Kind); + Assert.AreEqual(DateTimeKind.Local, c2.Value.Kind); + Assert.AreEqual(DateTimeKind.Unspecified, c3.Value.Kind); + Assert.AreEqual(DateTimeKind.Unspecified, c4.Value.Kind); + } + + [Test] + public void DeserializeFromJObject() + { + string json = @" + { + ""Value"": ""2017-12-05T21:59:00"" + }"; + + JObject jo = JObject.Parse(json); + + DateTimeWrapper c1 = jo.ToObject(JsonSerializer.Create(new JsonSerializerSettings + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc + })); + + DateTimeWrapper c2 = jo.ToObject(JsonSerializer.Create(new JsonSerializerSettings + { + DateTimeZoneHandling = DateTimeZoneHandling.Local + })); + + DateTimeWrapper c3 = jo.ToObject(JsonSerializer.Create(new JsonSerializerSettings + { + DateTimeZoneHandling = DateTimeZoneHandling.Unspecified + })); + + DateTimeWrapper c4 = jo.ToObject(); + + Assert.AreEqual(DateTimeKind.Utc, c1.Value.Kind); + Assert.AreEqual(DateTimeKind.Local, c2.Value.Kind); + Assert.AreEqual(DateTimeKind.Unspecified, c3.Value.Kind); + Assert.AreEqual(DateTimeKind.Unspecified, c4.Value.Kind); + } + + + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 23923262..2b354c0a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -990,6 +990,11 @@ namespace Newtonsoft.Json.Serialization } } + if (value is DateTime dt) + { + return DateTimeUtils.EnsureDateTime(dt, reader.DateTimeZoneHandling); + } + return value; }