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.
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?
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?
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.
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?
Help us improve MSDN.
Visit our UserVoice Page to submit and vote on ideas!