From 583eb120152f8b6332df2fe3d4b9f4c947c944d0 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 11 Mar 2021 21:42:55 +1300 Subject: [PATCH] Fix missing error when deserializing JToken with a contract type mismatch (#2494) --- Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs | 55 +++++++++++++++++++ .../Serialization/JsonSerializerTest.cs | 8 +-- .../JsonSerializerInternalReader.cs | 9 +++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs new file mode 100644 index 000000000..baa70f32f --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue2484.cs @@ -0,0 +1,55 @@ +#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 (NET45 || NET50) +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.Collections.Generic; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Converters; +using System.Collections; +using System; +using Newtonsoft.Json.Linq; + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue2484 + { + [Test] + public void Test() + { + var json = "[]"; + var ex = ExceptionAssert.Throws(() => JsonConvert.DeserializeObject(json, typeof(JObject))); + Assert.AreEqual("Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path '', line 1, position 2.", ex.Message); + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 372aa8c33..6ad965a35 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -3515,13 +3515,9 @@ public void CannotDeserializeArrayIntoLinqToJson() { string json = @"[]"; - ExceptionAssert.Throws( + ExceptionAssert.Throws( () => { JsonConvert.DeserializeObject(json); }, - new[] - { - "Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.", - "Cannot cast from source type to destination type." // mono - }); + "Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path '', line 1, position 2."); } [Test] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index e79565a45..53b39030e 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -238,6 +238,15 @@ private JsonSerializerProxy GetInternalSerializer() token = writer.Token; } + if (contract != null && token != null) + { + if (!contract.UnderlyingType.IsAssignableFrom(token.GetType())) + { + throw JsonSerializationException.Create(reader, "Deserialized JSON type '{0}' is not compatible with expected type '{1}'." + .FormatWith(CultureInfo.InvariantCulture, token.GetType().FullName, contract.UnderlyingType.FullName)); + } + } + return token; }