3

For this code...

private void Label1_MouseUp(object sender, RoutedEventArgs e)
{
    Newtonsoft.Json.Linq.JObject.FromObject(e).ToString();
}

I get this error...

Self referencing loop detected with type 'System.Windows.Documents.Run'. Path 'MouseDevice.Target.Inlines[0].SiblingInlines'.

There are plenty of other similar question, but I cant figure out how to implement the solutions in my case (I am learning C#). E.g. add ReferenceLoopHandling = ReferenceLoopHandling.Ignore But cant figure out where to put this.

(By the way, I'm trying to find a simple general-purpose way of printing out debug info.)

spiderplant0
  • 3,872
  • 12
  • 52
  • 91
  • IT looks this is a recurring issue with parent/child collections> here is a work around: http://stackoverflow.com/a/13549982/335905 – celerno Jun 20 '14 at 19:08
  • Thanks @celerno, the solution here seems to be similar to L.B.s – spiderplant0 Jun 20 '14 at 19:21
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:37

2 Answers2

4

In your question you said that you can't figure out where to put the ReferenceLoopHandling = ReferenceLoopHandling.Ignore. You can put this in your call to `FromObject' as follows:

JObject.FromObject(e, new JsonSerializer() {
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

Here's a link to the JObject.FromObject documentation: http://james.newtonking.com/json/help/index.html?topic=html/CreatingLINQtoJSON.htm

Captain Whippet
  • 2,143
  • 4
  • 25
  • 34
0

Yes, This is very common problem, and the error message say it all.

As you guess what you got is reference that already been serilize thus resulting in infinite action to recursion to serilize that object. If you will mark it as Ignore ReferenceLoopHandling the serilize will keep on working and will produce you a StackoverflowException.

Instead you have few solutions, here are 2 of them:

  • You can identify which of the properties is causing that loop, and set it on null if not needed.
  • You can create another class specific for prepare the serilize object, populate it, and than serilize it.
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36