2

In .NET 3.5, Is it possible to override the default DataContractJsonSerializer and use the JSON.net serializer instead?

NOTE: We do not want to use attributes on the class

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
Tawani
  • 11,067
  • 20
  • 82
  • 106

1 Answers1

3

Yes, it's possible to do so. But it's not too simple. You'll need a new message formatter which uses the JSON.NET serializer instead of the default one to convert between the operation parameters and the message object needed by the WCF stack. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx has an example that does exactly that. The code is written for 4.0, but it should work for 3.5 as well.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Nice article, but how would you handle XML requests? – Tawani Jun 15 '12 at 15:40
  • Yes, but you'd need to change the WebContentTypeMapper implementation to only return "raw" for JSON requests. The formatter would then be changed to first check the request format (via the WebBodyFormatMessageProperty); if it's XML (or not Raw), delegate the call to the original formatter, which can handle XML. – carlosfigueira Jun 15 '12 at 17:14