title: Json.net deserialize json with circular reference in MVC4
body: <p>I am trying with no luck to deserialize object graph with circular reference,</p>
  
  <p>Json.net works well when serializing circular references : adding a $id to objects and replacing objects with $ref = *objectId,
  When i send the same data back to the MVC action, it won't deserialize correctly - replacing the $refs with empty objects.</p>
  
  <p>I use json.net to both serialize and deserialize, deserializing is implemented in a custom IValueProvider - <a href="https://json.codeplex.com/discussions/347099" rel="noreferrer">https://json.codeplex.com/discussions/347099</a></p>
  
  <p>I noticed that when deserializing to an anonymous object </p>
  
  <pre><code>JsonConverter.Deserialize(json);
  </code></pre>
  
  <p>it will not deal with the circular references.
  but when deserializing using a generic type</p>
  
  <pre><code>JsonConverter.Deserialize&lt;EntityType&gt;(json) 
  </code></pre>
  
  <p>it will deserialize correctly.</p>
  
  <p>But i can't find the type in the GetValueProvider(ControllerContext controllerContext)</p>
  
  <p>any help will be appreciated</p>
  
  <p>Edit-
  In my current fix i pass the whole json as a string and use</p>
  
  <pre><code>JsonConverter.Deserialize&lt;EntityType&gt;(json) 
  </code></pre>
  
  <p>with the right type in the controller's action, but that's definitely not the right way to work with json + mvc4...
  I need a better way to integrate it into mvc, started a bounty</p>
  
  <p>Edit- More code
  The type :</p>
  
  <pre><code>[JsonObject(IsReference = true)]
  public class DynamicEntity : EntityWithID&lt;Guid&gt;
  {
      ....
  
      public virtual IList&lt;DynamicEntity&gt; ReferenceFields { get; set; }
  }
  </code></pre>
  
  <p>The json to deserialize is the output of the Serialize method of Json.net.</p>
  
  <pre><code>{"$id":"1","ReferenceFields":[{"$ref":"1"}],"Id":"9f9de7f3-865e-4511-aeac-a2ff01193b06"}
  </code></pre>
  
  <p>The issue is the integration with MVC because the json goes back and forth between the server and client.
  I already have js methods to change it back and forth to the same exact format - tested as i use it like this for now :</p>
  
  <pre><code>public ActionResult EntitySaveOrUpdate(string entity)
      {
          var entityToSave = JsonConvert.DeserializeObject&lt;DynamicEntity&gt;(entity);
          ...
      }
  </code></pre>
  
  <p>And it works just fine, but i need a better integration with MVC and not deserialize in all of my actions...</p>