diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs index 58fe2758..f84dc048 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs @@ -664,5 +664,21 @@ namespace Newtonsoft.Json.Tests.Bson } }); } + + [Test] + public void DateTimeZoneHandling() + { + MemoryStream ms = new MemoryStream(); + JsonWriter writer = new BsonWriter(ms) + { + DateTimeZoneHandling = Json.DateTimeZoneHandling.Utc + }; + + writer.WriteStartArray(); + writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); + writer.WriteEndArray(); + + Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray()))); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index ad0f37a9..ee34d5ba 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -751,5 +751,18 @@ now brown cow?", '"', true); Assert.AreEqual("Existing,Appended", p.Name); } #endif + + [Test] + public void SerializeObjectDateTimeZoneHandling() + { + string json = JsonConvert.SerializeObject( + new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified), + new JsonSerializerSettings + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc + }); + + Assert.AreEqual(@"""2000-01-01T01:01:01Z""", json); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index e9af6811..a1f2148f 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -824,5 +824,19 @@ _____'propertyName': NaN } } } + + [Test] + public void DateTimeZoneHandling() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw) + { + DateTimeZoneHandling = Json.DateTimeZoneHandling.Utc + }; + + writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); + + Assert.AreEqual(@"""2000-01-01T01:01:01Z""", sw.ToString()); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs index 3c360a8f..24eb6c55 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs @@ -174,5 +174,21 @@ namespace Newtonsoft.Json.Tests.Linq fail ]", writer.Token.ToString()); } + + [Test] + public void DateTimeZoneHandling() + { + JTokenWriter writer = new JTokenWriter + { + DateTimeZoneHandling = Json.DateTimeZoneHandling.Utc + }; + + writer.WriteValue(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); + + JValue value = (JValue) writer.Token; + DateTime dt = (DateTime)value.Value; + + Assert.AreEqual(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc), dt); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index c7cf16d0..465543f5 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -72,5 +72,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.2.14814")] +[assembly: AssemblyFileVersion("4.5.3.14817")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index c342c524..d817b4db 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -338,6 +338,29 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("[0] - 0 - Could not convert string to integer: a. Line 1, position 4.", errors[0]); Assert.AreEqual("[1] - 1 - Could not convert string to integer: b. Line 1, position 8.", errors[1]); } + + [Test] + public void ErrorHandlingAndAvoidingRecursiveDepthError() + { + string json = "{'A':{'A':{'A':{'A':{'A':{}}}}}}"; + JsonSerializer serializer = new JsonSerializer() { }; + IList errors = new List(); + serializer.Error += (sender, e) => + { + e.ErrorContext.Handled = true; + errors.Add(e.ErrorContext.Path); + }; + + serializer.Deserialize(new JsonTextReader(new StringReader(json)) { MaxDepth = 3 }); + + Assert.AreEqual(1, errors.Count); + Assert.AreEqual("A.A.A", errors[0]); + } + + public class Nest + { + public Nest A { get; set; } + } } [JsonObject] diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index b8e07384..e595009c 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -403,6 +403,7 @@ namespace Newtonsoft.Json.Bson public override void WriteValue(DateTime value) { base.WriteValue(value); + value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling); AddValue(value, BsonType.Date); } diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 70415a6f..11437865 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -436,9 +436,8 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(DateTime value) { - - base.WriteValue(value); + value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling); JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling); } diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index 2f89269e..a9d52b8f 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -348,6 +348,7 @@ namespace Newtonsoft.Json.Linq public override void WriteValue(DateTime value) { base.WriteValue(value); + value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling); AddValue(value, JsonToken.Date); } diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 74e793c5..37ae3b9a 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -88,7 +88,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.2.14814")] +[assembly: AssemblyFileVersion("4.5.3.14817")] #endif [assembly: CLSCompliant(true)] diff --git a/Tools/NuGet/NuGet.exe b/Tools/NuGet/NuGet.exe index 34ad49b4..79482f60 100644 Binary files a/Tools/NuGet/NuGet.exe and b/Tools/NuGet/NuGet.exe differ