Skip to content

Deserializing Existing Json with Basic Example Results in Exception #32

@jonmotos

Description

@jonmotos

Describe the bug
When using JsonKnownTypes on existing json content, adding a new derived type and then decorating the base class with the basic [JsonConverter...] attribute results in an exception:

To Reproduce
Steps to reproduce the behavior:

  1. Create a base class
  2. Serialize this class using standard Newtonsoft behaviors, no type handling
  3. Implement a new derived class
  4. Decorate the base class with the JsonConverter for JsonKnownTypes
  5. Deserialize

Expected behavior
I would expect an instance without a discriminator to automatically deserialize to the base class

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Exception content:
JsonKnownTypes.Exceptions.JsonKnownTypesException: discriminator is not registered for T type

I then tried adding [JsonKnownTypeFallback(typeof(Base))]
This results in another exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'Base' to type 'Derived'.

Two unexpected behaviors here!

I would be very happy to help implement a solution here!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions