-Fixed error when writing unknown null value as JSON
This commit is contained in:
@@ -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<Type> types = new List<Type>
|
||||
{
|
||||
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<Type, object> types = new Dictionary<Type, object>
|
||||
{
|
||||
[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()
|
||||
{
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Type, JsonContract> _contractCache;
|
||||
|
||||
Reference in New Issue
Block a user