0

Is it possible to make JSON the default serializer in WebApi2 without removing the XML one?

I've tried to order XmlSerializer to back and JsonSerializer to front but nothing seems to change.

2
  • My understanding is that it has more to do with what the client sends in the accept header. If XML is the first thing in the accept header and the XML serializer is present that's what you'll get. Commented Apr 23, 2014 at 21:26
  • @CraigW. But what if client sends nothing at all? Commented Apr 23, 2014 at 21:30

1 Answer 1

1

By default the json formatter should "win". There are a few things determining what formatter is going to "win".

  1. Can the JSON formatter even write your type, you might want to verify that first. If you use a DataContractSerializer it's a bit more picky, the json.net version is behaving nicer (and it's default).
  2. If you have no AcceptHeader, the content negotiator still looks at the content type of the request and will prefer that. Is your request sending XML?

If both of these questions are not the issue, the content negotiator (DefaultContentNegotiator) will now end up with the XML and the JSON formatters and call SelectResponseMediaTypeFormatter

Here basically it will pick the first formatter on the list (I'm saying it with a caviat as it's a bit more complex than that) so you want to verify your list is actually re-ordered as you expected it.

Lastly and I don't think you need to get there as the stuff above should get you fixed, you can always replace the IContentNegotiator and override SelectResponseMediaTypeFormatter where if you have more than one formatter you choose the Json one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.