From 90976eb6ededdd07c34293c325ed32462745da4c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 22 Oct 2017 19:26:57 +1300 Subject: [PATCH] -Fixed error when writing unknown null value as JSON --- .../Converters/DataTableConverterTests.cs | 102 ++++++++++++++++++ Src/Newtonsoft.Json.Tests/Issues/Issue1460.cs | 64 +++++++++++ Src/Newtonsoft.Json/JsonWriter.cs | 7 ++ .../Serialization/DefaultContractResolver.cs | 1 - 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue1460.cs diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs index da2a2502..5a989274 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs @@ -39,12 +39,114 @@ using Assert = Newtonsoft.Json.Tests.XUnitAssert; using NUnit.Framework; #endif using System.Data; +using System.Data.SqlTypes; +using System.Linq; using Newtonsoft.Json.Tests.TestObjects; +using System.Linq; +#if !(NET20 || NET35) +using System.Numerics; +#endif namespace Newtonsoft.Json.Tests.Converters { public class DataTableConverterTests : TestFixtureBase { +#if !(NET20 || NET35) + [Test] + public void SerializeNullValues() + { + DataTable dt = new DataTable(); + List types = new List + { + typeof(TimeSpan), + typeof(char[]), + typeof(Type), + typeof(Object), + typeof(byte[]), + typeof(Uri), + typeof(Guid), + typeof(BigInteger) + }; + + foreach (var ss in types) + { + dt.Columns.Add(ss.Name, ss); + } + + dt.Rows.Add(types.Select(t => (object)null).ToArray()); + + StringWriter sw = new StringWriter(); + JsonTextWriter jsonWriter = new JsonTextWriter(sw); + jsonWriter.Formatting = Formatting.Indented; + + DataTableConverter converter = new DataTableConverter(); + converter.WriteJson(jsonWriter, dt, new JsonSerializer()); + + StringAssert.AreEqual(@"[ + { + ""TimeSpan"": null, + ""Char[]"": null, + ""Type"": null, + ""Object"": null, + ""Byte[]"": null, + ""Uri"": null, + ""Guid"": null, + ""BigInteger"": null + } +]", sw.ToString()); + } + + [Test] + public void SerializeValues() + { + DataTable dt = new DataTable(); + Dictionary types = new Dictionary + { + [typeof(TimeSpan)] = TimeSpan.Zero, + [typeof(char[])] = new char[] {'a', 'b', 'c' }, + [typeof(Type)] = typeof(string), + [typeof(Object)] = new object(), + [typeof(byte[])] = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, + [typeof(Uri)] = new Uri("http://localhost"), + [typeof(Guid)] = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), + [typeof(BigInteger)] = BigInteger.Parse("10000000000000000000000000000000000") + }; + + foreach (var ss in types) + { + dt.Columns.Add(ss.Key.Name, ss.Key); + } + + dt.Rows.Add(types.Select(t => t.Value).ToArray()); + + StringWriter sw = new StringWriter(); + JsonTextWriter jsonWriter = new JsonTextWriter(sw); + jsonWriter.Formatting = Formatting.Indented; + + DataTableConverter converter = new DataTableConverter(); + converter.WriteJson(jsonWriter, dt, new JsonSerializer()); + + string stringName = typeof(string).AssemblyQualifiedName; + + StringAssert.AreEqual(@"[ + { + ""TimeSpan"": ""00:00:00"", + ""Char[]"": [ + ""a"", + ""b"", + ""c"" + ], + ""Type"": """ + stringName + @""", + ""Object"": {}, + ""Byte[]"": ""AQIDBAUGBwg="", + ""Uri"": ""http://localhost"", + ""Guid"": ""00000001-0002-0003-0405-060708090a0b"", + ""BigInteger"": 10000000000000000000000000000000000 + } +]", sw.ToString()); + } +#endif + [Test] public void WriteJsonNull() { diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1460.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1460.cs new file mode 100644 index 00000000..fa4340f5 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1460.cs @@ -0,0 +1,64 @@ +#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.Diagnostics; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Xml; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Utilities; +#if !NET20 +using System.Xml.Linq; +#endif +#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 Issue1460 : TestFixtureBase + { + [Test] + public void Test() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + JsonWriter.WriteValue(writer, PrimitiveTypeCode.Object, null); + + Assert.AreEqual("null", sw.ToString()); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index ade6d6f5..91346c0e 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -1631,6 +1631,13 @@ namespace Newtonsoft.Json break; } #endif + // write an unknown null value, fix https://github.com/JamesNK/Newtonsoft.Json/issues/1460 + if (value == null) + { + writer.WriteNull(); + break; + } + throw CreateUnsupportedTypeException(writer, value); } } diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 343cff68..ede251e0 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -93,7 +93,6 @@ namespace Newtonsoft.Json.Serialization new RegexConverter() }; - private readonly object _typeContractCacheLock = new object(); private readonly PropertyNameTable _nameTable = new PropertyNameTable(); private readonly ThreadSafeStore _contractCache;