JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

Deserializing object of missing class as it's Base class (C#) #2972

Closed GuyDayannn closed 2 months ago

GuyDayannn commented 2 months ago

Hi, I wanted to ask if the follow scenario possible. I'm developing an app that uses a plugin so it loads derived class of a base class from a dll.

For example, I my code base has a base class:

public class BaseClass
{
  // some properties
}

and the derived class comes from a dll that loads at runtime:

public class DerivedClass : BaseClass
{
  // some properties
}

I'm using TypeNameHandling = TypeNameHandling.All for serializing and deserializing When I serialize and deserialize objects using the DLL, everything works fine. However, when I remove the DLL and attempt to deserialize a file containing derived class objects, deserialization fails. Is there an option to ensure that deserialization does not fail, but instead treats the derived class as the base class?

Thanks for the help, Guy.

GuyDayannn commented 2 months ago

We used JsonSerializerSettings Error = HandleDeserializationError property