diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index a9c95b33..e3e8b985 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -1225,5 +1225,39 @@ keyword such as type of business."" GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject(json); } + + public class Co : ICo + { + public String Name { get; set; } + } + + public interface ICo + { + String Name { get; set; } + } + + public interface ITest + { + ICo co { get; set; } + } + + public class Test : ITest + { + public ICo co { get; set; } + + public Test() + { + } + } + + [Test] + [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Could not create an instance of type Newtonsoft.Json.Tests.JsonSerializerTest+ICo. Type is an interface or abstract class and cannot be instantated.")] + public void DeserializeInterfaceProperty() + { + Test testClass = new Test(); + testClass.co = new Co(); + String strFromTest = JsonConvert.SerializeObject(testClass); + Test testFromDe = (Test)JsonConvert.DeserializeObject(strFromTest, typeof(Test)); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 92871ac7..2c0832c9 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -493,7 +493,11 @@ namespace Newtonsoft.Json { object newObject; - if (ReflectionUtils.HasDefaultConstructor(objectType)) + if (objectType.IsInterface || objectType.IsAbstract) + { + throw new JsonSerializationException("Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantated.".FormatWith(CultureInfo.InvariantCulture, objectType)); + } + else if (ReflectionUtils.HasDefaultConstructor(objectType)) { newObject = Activator.CreateInstance(objectType);