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)
4.3 Star
12,674 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • We found serval errors
    2 Posts | Last post April 05, 2015
    • Hi Henrik,
           We downloaded the demo code  and upgrade it to  webapi 2.0, there were serval errors.
      Havd you found it?
      MSDN CHina Club QQ Group:29754721 (微软MSDN技术俱乐部 )
    • The solution:
  • 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?
  • 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.
              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.
    • I just found this blog post that explains a workaround, it seems this issue may be fixed post-beta, correct?
  • 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?