Giter Site home page Giter Site logo

Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated. about newtonsoft.json HOT 2 CLOSED

benjamin-achouche avatar benjamin-achouche commented on August 24, 2024
Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated.

from newtonsoft.json.

Comments (2)

elgonzo avatar elgonzo commented on August 24, 2024

The error is because all the deserializer knows is that the deserialization target type for some json object(s) is the interface IEpisodeSaveData which obviously is not type that can be instantiated. The deserializer also doesn't start reading the tea-leaves, trying to guess what you expect it to do with this dictionary having IEpisodeSaveData as its value type. So, it basically says "Nope. I have no clue what you want. No soup for you."

Your best way forward is probably to let the serializer put type metadata into the serialized json data, so the deserializer will know from this metadata which types to create. To do this, you will not only have to use TypeNameHandling = TypeNameHandling.Auto for the deserializer, but you will also have to use it for the serializer.

(the issue lies solely in the "eventFlags" prop)

No, it doesn't. It lies chiefly with the Dictionary<Character, IEpisodeSaveData> episodes dictionary. Note that once the deserializer would know which concrete IEpisodeSaveData type to use for a given json object, it would automatically also know the concrete Dictionary<T, bool> eventFlags type, because the concrete type for T is part of the type definition of the concrete IEpisodeSaveData classes. :-)

from newtonsoft.json.

benjamin-achouche avatar benjamin-achouche commented on August 24, 2024

Yeah that seemed to have fixed it ! Thanks !

from newtonsoft.json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.