I have a function that serializes objects. It works fine in every case, except when I run a specific object. This is an object of the class that contains Tasks. But I do not understand why this will be a problem.
In debug mode, the code simply gets stuck without any error or any exception. Just stop and wait. We also mention that this serialization is called at the start of the Task, but I'm also not sure why this will be a problem.
I also set the [NonSerialized] attribute for all Task properties, but still nothing.
[NonSerialized]
private Task<bool> isConnected;
public Task<bool> IsConnected
{
get { return isConnected; }
set { isConnected = value; }
}
This is my function:
public static string ToJson(this object obj)
{
try
{
var res = JsonConvert.SerializeObject(obj, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return res;
}
catch
{
return $"Object {obj.ToString()} is not serializable.";
}
}
This is the object I'm trying to serialize:

source
share