Fix missing error when deserializing JToken with a contract type mismatch (#2494)

This commit is contained in:
James Newton-King
2021-03-11 21:42:55 +13:00
committed by GitHub
parent b6dc05be5a
commit 583eb12015
3 changed files with 66 additions and 6 deletions
@@ -238,6 +238,15 @@ namespace Newtonsoft.Json.Serialization
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;
}