raygun4net icon indicating copy to clipboard operation
raygun4net copied to clipboard

When serializing a Uri property in type it serializes as [Circular reference detected, object not serialized]

Open tully2003 opened this issue 8 years ago • 1 comments

I noticed that anytime an object contained an URI it would be serialized as [Circular reference detected, object not serialized].

I am using v5.3.1 and was able to recreate when running that code, the below error should allow you to experience the issue.

[Test]
public void UriSerializingAsCircularReference()
{
    Request r = new Request { Uri = new Uri("https://api.twitter.com") };

    string json = SimpleJson.SerializeObject(r);

    Assert.AreEqual("{\"Uri\":\"https://api.twitter.com\"}", json); // expected
    Assert.AreEqual("{\"Uri\":\"[Circular reference detected, object not serialized]\"}", json); // actual
}

public class Request
{
    public Uri Uri { get; set; }
}

After digging through the source the SerializeValue function in SimpleJson.cs (line 1016) is recursing back into itself adding Uri onto the visited stack again. The code that appears to be the culprit is located at line 1074 and is reproduced below.

object serializedObject;
success = jsonSerializerStrategy.TrySerializeNonPrimitiveObject(value, out serializedObject);
if (success)
    SerializeValue(jsonSerializerStrategy, serializedObject, builder, visited);

tully2003 avatar Sep 19 '16 11:09 tully2003

Thanks for pointing this out to us. We'll look into fixing this for the next version.

QuantumNightmare avatar Sep 19 '16 22:09 QuantumNightmare