Using JSON.NET with ASP.NET Web API

Json.Net is a popular framework for working with JSON. In particular, it has a bunch of features that are not supported by the DataContractJsonSerializer such as being much more flexible in what kind of types it can serialize and exactly how they should be serialized.

C# (6.9 MB)
 
 
 
 
 
(6)
10,112 times
Add to favorites
9/1/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • System.Net.Http (beta) Nuget Package
    1 Posts | Last post January 06, 2013
    • Hi all
      
      I have tried to use the JsonNetFormatter class in my project.  However, I can't resolve some of the missing references.  I looked in the sample project and noticed it's referencing the System.Net.Http nuget package (and others), but when I search in NuGet I can't find it and add it to my solution.
      
      This means I can't find a reference for IKeyValueModel and Encoding in the class - probably other dependent on the System.Net.Http Namespace too.
      
      Do you have any ideas?
      
      Thanks
      
      Mark
  • parameter binding
    2 Posts | Last post March 09, 2012
    • I have an issue when a method contains primitives and a model that needs to be bound from incoming JSON.
      
      Example:
      
              [HttpPut]
              public void Receiving(int id, MyModel model)
              {
      
              }
      
      The JSON deserializer tries to deserialize the "int id" parameter but doesn't know what to do.  I'm trying to figure out how to get the id to bind from the route, while getting the model to bind from the JSON sent in the request.
      
      Thanks,
      
      --Jeff
      
    • I just found this blog post that explains a workaround, it seems this issue may be fixed post-beta, correct?
      
      http://geekswithblogs.net/jdothoffman/archive/2012/03/06/mvc-4-beta-web-api-bug-with-binding-route-values.aspx
      
      Thanks,
      
      --Jeff
  • Inconsistent with built-in JSON media type formatter
    2 Posts | Last post March 05, 2012
    • I ran into a few issues when trying to use this code because it doesn't behave like the Web API framework expects.
      
      First, the underlying stream is being disposed, which makes it unavailable for other components in the pipeline. I solved this by not wrapping the StreamReader or StreamWriter in a using block and passing CloseInput = false to the JsonTextReader. Because of that change I also had to make sure to flush the StreamWriter.
      
      Second, doing the serialization on a new Task causes the controller action to be processed on a different thread than the message handlers and filters. This may be a bug in the framework, but the built-in JSON serializer actually runs the serialization synchronously, which causes it to use the correct thread. I had to implement something like what the JsonMediaTypeFormatter does with executing synchronously and returning an already-completed Task to get this to work.
    • After looking into this some more my second point above is probably not an issue. The built-in formatter does run synchronously but there should be no issue with doing it asynchronously.
  • XElement to JSON
    1 Posts | Last post March 02, 2012
    • Hi,
      I have an API that returns an XElement, and I was hoping to use a JSON MediaTypeFormatter to convert this XElement to JSON. However, I have used your code and a breakpoint in OnWriteToStreamAsync is never hit for the XElement. It goes into CanWriteType, which returns "true". Why would it not get into OnWriteToStreamAsync?
      
      Thanks