title: Set a maximum depth of JSON serialization in .NET Web API
body: <p>Let's say I have these entitites with these relations: <em>This is just a fictious example, and not my current entities.</em></p>
  
  <ul>
  <li>Course</li>
  <li>User</li>
  <li>NewsPosts</li>
  </ul>
  
  <p>Courses have many Users,
  Users have many Courses</p>
  
  <p>Courses has many NewsPosts,
  NewsPosts has many Courses</p>
  
  <p>Users has many NewsPosts,
  NewsPosts has Many Users</p>
  
  <p>I'm using Entity Framework code first with .NET Web API, which sends back entities in the form of JSON. When I try to Get a Course, it sends back a JSON result with the relations of the entites, which is fine, but I would wish to set a limit of how many levels it serializes so it does not serialize all the relations beyond the first or second level. </p>
  
  <p>GET Course/ would be serialized to:</p>
  
  <pre><code>   {
       "Users":[{
                  "id":1,
                  "newsPosts": [{
                                  "id":1,
                                  "message":"foo"
                               }]
               }],
     "newsPosts":[{
                   "id":2,
                   "message":"bar"
                 }]   
  
     }
  </code></pre>
  
  <p>What i would want is to serialize only 1 or maybe 2 levels, So that the result would be:</p>
  
  <pre><code>   {
       "Users":[{
                  "id":1,
                  "newsPosts": []
               }],
     "newsPosts":[{
                   "id":2,
                   "message":"bar"
                 }]   
  
     }
  </code></pre>
  
  <p>I have added a:</p>
  
  <pre><code>    json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  </code></pre>
  
  <p>This is to remove the referencelooping.</p>
  
  <p>So in short: Is there any way of setting a maximum amount of nodes to serialize?</p>