title: How Do You "Really" Serialize Circular Referencing Objects With Newtonsoft.Json?
body: <p>I'm having a problem getting some data serialized correctly from my ASP.NET Web API controller using Newtonsoft.Json.  </p>
  
  <p>Here's what I <em>think</em> is going on - please correct me if I'm wrong.  Under certain circumstances (specifically when there aren't any circular references in the data) everything works just like you'd expect - a list of populated objects gets serialized and returned.  If I introduce data that causes a circular reference in the model (described below, and even with <code>PreserveReferencesHandling.Objects</code> set) only the elements of the list leading up to the first object with a circular reference get serialized in a way that the client can "work with".  The "elements leading up to" can be any of the elements in the data if it's ordered differently before sending things to the serializer, but at least one will be serialized in a way the client can "work with".  The empty objects end up being serialized as Newtonsoft references (<code>{$ref:X}</code>).</p>
  
  <p>For example, if I have an EF model complete with navigation properties that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/dRMsl.jpg" alt="Model"></p>
  
  <p>In my global.asax:</p>
  
  <pre><code>var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
  json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
  </code></pre>
  
  <p>Here's the fundamental query I'm doing using Entity Framework (lazy-loading is off so there aren't any proxy classes here):</p>
  
  <pre><code>[HttpGet]
  [Route("starting")]
  public IEnumerable&lt;Balance&gt; GetStartingBalances()
  {
     using (MyContext db = new MyContext())
     {
         var data = db.Balances
          .Include(x =&gt; x.Source)
          .Include(x =&gt; x.Place)
          .ToList()
         return data;
      }
  }
  </code></pre>
  
  <p>So far so good, <code>data</code> is populated.</p>
  
  <p>If there are no circular references, life is grand.  However, as soon as there are 2 <code>Balance</code> entities with the same <code>Source</code> or <code>Place</code>, then the serialization turns the later <code>Balance</code> objects of the top-most list that I'm returning into Newtonsoft references instead of their full-fledged objects because they were already serialized in the <code>Balances</code> property of the <code>Source</code> or <code>Place</code> object(s):</p>
  
  <pre><code>[{"$id":"1","BalanceID":4,"SourceID":2,"PlaceID":2 ...Omitted for clarity...},{"$ref":"4"}]
  </code></pre>
  
  <p>The problem with this is that the client doesn't know what to do with <code>{$ref:4}</code> even though we humans understand what's going on.  In my case, this means that I cannot use AngularJS to <code>ng-repeat</code> over my entire list of Balances with this JSON, because they aren't all true <code>Balance</code> objects with a <code>Balance</code> property to bind.  I'm sure there are tons of other use-cases that would have the same problem.</p>
  
  <p>I can't turn off the <code>json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects</code> because lots of other things would break (which is well-documented in 100 other questions here and elsewhere).  </p>
  
  <p>Is there a better workaround for this apart from going through the entities in the Web API controller and doing </p>
  
  <pre><code>Balance.Source.Balances = null;
  </code></pre>
  
  <p>to all of the navigation properties to break the circular references?  Because THAT doesn't seem right either.</p>