I have a third-party class (lets call it Class1
) which I need to serialize to JSON. If I try to do this as is, I either receive StackOverflowException
or JsonSerializationException
with message "Self referencing loop detected with type". I've tried to apply the following settings for the JsonConvert
but it didn't help me to avoid StackOverflowException
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None,
PreserveReferencesHandling = PreserveReferencesHandling.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
After decompiling the Class1
I found out that a lot of properties of the Class1
are marked with [ScriptIgnore]
attribute which is an analogue of [JsonIgnore]
and is used by System.Web.Script.Serialization.JavaScriptSerializer
but I need to use Newtonsoft serializer.
As far as Class1
is a third-party class I can't add [JsonIgnore]
attribute to the needed properties.
I know that I can develop my own implementation of IContractResolver
, and handle the problematic properties there, but I'd like to avoid this option.
Maybe there is a way somehow configure Newtonsoft serializer to take into consideration [ScriptIgnore]
attribute as well as [JsonIgnore]
. And do this configuration like it is done with ReferenceLoopHandling
?
I would appreciate for any ideas.