5

Is there a way to configure what JSON serializer is used when returning JSON via the JsonResult in the controller:

public ActionResult SomeJsonFunction()
{
  var x = SomeModelCode.SomeModelFunction();
  return Json(x);
}

It looks like the default is the JavaScriptSerializer. I would love to be able to use the DataContractJsonSerializer, but cannot find any documentation on how to do this.

1 Answer 1

5

Check the source to see how JsonResult is implemented. Derive from ActionResult with your DataContractJsonSerializer implementation. Right now it won't be easy to use the Json() helper method, but you could create your own helper method in a layer supertype controller. Your new helper method would return your new ActionResult derivation.

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

2 Comments

That is exactly what I ended up doing. It just instantiates the JavascriptSerializer, and is fairly trivial to write an ActionResult derived DataContractJsonResult.
Thanks a bunch! This solved a problem we were having with circular references and the other fallbacks of the JavaScriptSerializer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.