From 3274b9ab67f8a7d18b26832fcaf84037c4f926d7 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 1 Jan 2018 20:38:31 +1300 Subject: [PATCH] -Fixed serializing nullable collections --- Src/Newtonsoft.Json.Tests/Issues/Issue1512.cs | 78 +++++++++++++++++++ .../Serialization/DefaultContractResolver.cs | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue1512.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1512.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1512.cs new file mode 100644 index 00000000..09b7bddb --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1512.cs @@ -0,0 +1,78 @@ +#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 + +#if !(NET20 || NET35 || NET40) +using System; +using System.Collections.Immutable; +#if DNXCORE50 +using System.Reflection; +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 Issue1512 : TestFixtureBase + { + [Test] + public void Test() + { + var json = @"[ + { + ""Inners"": [] + } + ]"; + ImmutableArray result = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(1, result.Length); + Assert.AreEqual(0, result[0].Inners.Value.Length); + } + } + + public sealed class Outer + { + public Outer(ImmutableArray? inners) + { + this.Inners = inners; + } + + public ImmutableArray? Inners { get; } + } + + public sealed class Inner + { + public Inner(string id) + { + this.Id = id; + } + + public string Id { get; } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 0d9a41c6..f37c079b 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -1179,7 +1179,7 @@ namespace Newtonsoft.Json.Serialization if (typeof(IEnumerable).IsAssignableFrom(t)) { - return CreateArrayContract(objectType); + return CreateArrayContract(t); } if (CanConvertToString(t))