{"action":"created","issue":{"url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/682","labels_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/682/labels{/name}","comments_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/682/comments","events_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/682/events","html_url":"https://github.com/JamesNK/Newtonsoft.Json/issues/682","id":110794365,"number":682,"title":"TypeNameHandling not working on deserialization","user":{"login":"JoeyFR","id":6731436,"avatar_url":"https://avatars.githubusercontent.com/u/6731436?v=3","gravatar_id":"","url":"https://api.github.com/users/JoeyFR","html_url":"https://github.com/JoeyFR","followers_url":"https://api.github.com/users/JoeyFR/followers","following_url":"https://api.github.com/users/JoeyFR/following{/other_user}","gists_url":"https://api.github.com/users/JoeyFR/gists{/gist_id}","starred_url":"https://api.github.com/users/JoeyFR/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoeyFR/subscriptions","organizations_url":"https://api.github.com/users/JoeyFR/orgs","repos_url":"https://api.github.com/users/JoeyFR/repos","events_url":"https://api.github.com/users/JoeyFR/events{/privacy}","received_events_url":"https://api.github.com/users/JoeyFR/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-10-10T13:13:48Z","updated_at":"2015-10-11T06:30:46Z","closed_at":"2015-10-10T19:54:43Z","body":"I went through the questions on the subject here and on SO, and [the closest to my situation][1] didn't address my concern.\r\n\r\nI have the following classes :\r\n\r\n    public abstract class BaseClass\r\n    {\r\n    \r\n    }\r\n    \r\n    public class ConcreteClass\r\n    {\r\n    \r\n    }\r\n\r\nMy setting object for both the serialization and the deserialization is the following one : \r\n\r\n    JsonSerializerSettings _serializationSettings = new JsonSerializerSettings \r\n    { \r\n    \tNullValueHandling = NullValueHandling.Ignore, \r\n    \tTypeNameHandling = TypeNameHandling.All, \r\n    \tContractResolver = new MyContractResolver(), \r\n    \tConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, ReferenceLoopHandling = ReferenceLoopHandling.Ignore \r\n    };\r\n\r\nI am trying to deserialize like that :\r\n\r\n    var myDeserializedObject = JsonConvert.DeserializeObject<BaseClass>(jsonString);\r\n\r\nBut for some reason, I am getting the error \r\n\r\n> Could not create an instance of type T Type is an interface or abstract class and cannot be instantiated\r\n\r\nEven though the root Json object does have a $type property. I've tried deserializing to a JObject and then using `JObject.To<BaseType>()` but I'm having the same result. I need to get this approach working, and would prefer not to use custom converters as I use polymorphism all over the place.\r\n\r\nDo you have any idea on how I can get this deserialization working ?\r\n\r\nThanks\r\n\r\n\r\n**Update 10/10/15**\r\n\r\nI am still investigating and I think that the issue might be that when I inspect the JObject of my deserialized object, the First property is the \"_id\" property :\r\n\r\n[![enter image description here][3]][3]\r\n\r\nI assume that since the error message is : \r\n\r\n[![enter image description here][2]][2]\r\n\r\nJSON.NET probably needs to read the type first to instantiate the correct object. I don't see how to reproduce this situation where \"_id\" is first, from a separate project like the one provided below. I tried, a couple of combination, of nested complex properties, but I always have the $type first. And it is probably why it works fine there.\r\n\r\nI first tryied to put together an override of `CreateProperties` on my ContractResolver :\r\n\r\n    protected override System.Collections.Generic.IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)\r\n        {\r\n            var properties = base.CreateProperties(type, memberSerialization);\r\n\r\n            var propWithDollar = properties.Where(x => x.PropertyName.Contains(\"$\"));\r\n            foreach (var prop in propWithDollar)\r\n            {\r\n                properties.Remove(prop);\r\n                properties.Insert(0, prop);\r\n            }\r\n\r\n            return properties;\r\n        }\r\n\r\nThat was never called on deserialization, and finally I managed to do put the $type property at the very top by using : \r\n\r\n    var prop = deserializedJObject.Property(\"$type\");\r\n    deserializedJObject.Remove(\"$type\");\r\n    deserializedJObject.AddFirst(prop);\r\n\r\nBut unfortunately it didn't help, I'm still facing the same cast issue.\r\n\r\nI'm getting in serious troubles guys... I spent 4 days on that one. Any idea or suggestion would be very welcome.\r\n\r\n  [1]: http://stackoverflow.com/questions/28128923/serializing-an-interface-abstract-objet-using-newtonsoft-json\r\n  [2]: http://i.stack.imgur.com/5R3J0.png\r\n  [3]: http://i.stack.imgur.com/wJ18a.png"},"comment":{"url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/comments/147163809","html_url":"https://github.com/JamesNK/Newtonsoft.Json/issues/682#issuecomment-147163809","issue_url":"https://api.github.com/repos/JamesNK/Newtonsoft.Json/issues/682","id":147163809,"user":{"login":"JoeyFR","id":6731436,"avatar_url":"https://avatars.githubusercontent.com/u/6731436?v=3","gravatar_id":"","url":"https://api.github.com/users/JoeyFR","html_url":"https://github.com/JoeyFR","followers_url":"https://api.github.com/users/JoeyFR/followers","following_url":"https://api.github.com/users/JoeyFR/following{/other_user}","gists_url":"https://api.github.com/users/JoeyFR/gists{/gist_id}","starred_url":"https://api.github.com/users/JoeyFR/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoeyFR/subscriptions","organizations_url":"https://api.github.com/users/JoeyFR/orgs","repos_url":"https://api.github.com/users/JoeyFR/repos","events_url":"https://api.github.com/users/JoeyFR/events{/privacy}","received_events_url":"https://api.github.com/users/JoeyFR/received_events","type":"User","site_admin":false},"created_at":"2015-10-11T06:30:46Z","updated_at":"2015-10-11T06:30:46Z","body":"Thanks James! I think this should be in the doc page of TypeNameHandling. As a person new to the library, it took me 4 days to track my issue down  to the JSON string.\r\n\r\nAnd I mean 4 days of reading all the SO questions, reading the source code etc... Before that you are in the dark and nobody can help you"}}