Make default XML and JSON formatters consistent so that conneg works


A key part of Content Negotiation is that our default XML and JSON serializers need compatible behavior.
Not doing this means that user code (or serializers) may crash depending on the accept/response headers on the request message. Eg, I build and test my WebAPI using JSON, and then start crashing when clients request XML.

Since the formatters here are two totally separate serializers (JSON.Net and DCS), it's not surprising they have different behavior regarding things like anonymous types, derived properties, etc.

We have a bunch of incompatibilities.

I checked in a test that excercises these. There are a bunch of failing tests (have the Skip attribute). See test\System.Net.Http.Formatting.Test.Unit\Formatting\SerializerConsistencyTests.cs

And this changelist:

The bug here is to fix the failing tests (and ideally expand the test coverage to find other issues).
Closed Mar 4, 2013 at 11:42 PM by HongmeiG
It is hard to guarantee with content negotiation, esp. when someone writes a custom formatter. For example, different formatters will behave differently with as they honor different attributes. If one action is exposed via XML and JSON format, then it is users responsibility to test both scenarios thoroughly.


rodrigo83 wrote Apr 21, 2012 at 12:53 AM

I have found issues with datetime serialization fields on JSON serializer (formatter).
I think that the impact is high becasue require a lot of workaround in order to get a good results on yours controllers.