Skip to content

Commit 583eb12

Browse files
authored
Fix missing error when deserializing JToken with a contract type mismatch (#2494)
1 parent b6dc05b commit 583eb12

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
#if (NET45 || NET50)
27+
#if DNXCORE50
28+
using Xunit;
29+
using Test = Xunit.FactAttribute;
30+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
31+
#else
32+
using NUnit.Framework;
33+
#endif
34+
using System.Collections.Generic;
35+
using Newtonsoft.Json.Serialization;
36+
using Newtonsoft.Json.Converters;
37+
using System.Collections;
38+
using System;
39+
using Newtonsoft.Json.Linq;
40+
41+
namespace Newtonsoft.Json.Tests.Issues
42+
{
43+
[TestFixture]
44+
public class Issue2484
45+
{
46+
[Test]
47+
public void Test()
48+
{
49+
var json = "[]";
50+
var ex = ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject(json, typeof(JObject)));
51+
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);
52+
}
53+
}
54+
}
55+
#endif

Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -3515,13 +3515,9 @@ public void CannotDeserializeArrayIntoLinqToJson()
35153515
{
35163516
string json = @"[]";
35173517

3518-
ExceptionAssert.Throws<InvalidCastException>(
3518+
ExceptionAssert.Throws<JsonSerializationException>(
35193519
() => { JsonConvert.DeserializeObject<JObject>(json); },
3520-
new[]
3521-
{
3522-
"Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.",
3523-
"Cannot cast from source type to destination type." // mono
3524-
});
3520+
"Deserialized JSON type 'Newtonsoft.Json.Linq.JArray' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path '', line 1, position 2.");
35253521
}
35263522

35273523
[Test]

Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs

+9
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ private JsonSerializerProxy GetInternalSerializer()
238238
token = writer.Token;
239239
}
240240

241+
if (contract != null && token != null)
242+
{
243+
if (!contract.UnderlyingType.IsAssignableFrom(token.GetType()))
244+
{
245+
throw JsonSerializationException.Create(reader, "Deserialized JSON type '{0}' is not compatible with expected type '{1}'."
246+
.FormatWith(CultureInfo.InvariantCulture, token.GetType().FullName, contract.UnderlyingType.FullName));
247+
}
248+
}
249+
241250
return token;
242251
}
243252

0 commit comments

Comments
 (0)