Change Default JSON Serializer Used In ASP MVC3 [d

2019-01-17 07:29发布

问题:

This question already has an answer here:

  • Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible? 7 answers

I have a controller that is returning large JSON objects to jQuery Flot and I was wondering how easy it would be to replace the default JavaScriptSerializer with something faster like the one from ServiceStack.Text.

It would be good if I could change stuff like this using the DependencyResolver, but I suppose if absolutely everything was resolved this was, it could get pretty slow.

回答1:

your best bet is to inherit from JsonResult class and override Execute method like

public class CustomJsonResult: JsonResult
{
    public CustomJsonResult()
    {
       JsonRequestBehavior = JsonRequestBehavior.DenyGet;
    }
    public override void ExecuteResult(ControllerContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType)) {
                response.ContentType = ContentType;
            }
            else {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null) {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null) {
                CustomJsSerializer serializer = new CustomJsSerializer();
                response.Write(serializer.Serialize(Data));
            }
        }
}

code is taken from JsonResult class of mvc3 and changed this line

JavaScriptSerializer serializer = new JavaScriptSerializer();

to

CustomJsSerializer serializer = new CustomJsSerializer();

you can use this class in action method like

public JsonResult result()
{
    var model = GetModel();
    return new CustomJsonResult{Data = model};
}

Additionally you can override json method of Controller class in your Base controller like

public class BaseController:Controller
{
   protected internal override JsonResult Json(object data)
        {
            return new CustomJsonResult { Data = data };
        }
}

now if you have all your controllers from BaseController then return Json(data) will call your serialization scheme. There are also other overloads of Json method that you may choose to override.



回答2:

I'm adding this answer simply because I'm using an alternate solution that doesn't require overriding the System.Web.Mvc.Controller class. I add the following extension methods to the System.Web.Mvc.Controller class. The only "benefit" of this solution is that it doesn't require you to change the base class of the code generated Controller classes. Otherwise, it is functionally equivalent to the accepted answer.

public static JsonResult ToJsonResult(this Controller controller, 
                                          object target, 
                                          string contentType, 
                                          Encoding contentEncoding,
                                          JsonRequestBehavior behavior)
    {
        if (target != null)
        {
            if (target.GetType().HasAttribute<DataContractAttribute>())
            {
                return new DataContractJsonResult() { ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, Data = target };
            }
        }
        return new JsonResult() { ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, Data = target };
    }

    public static JsonResult ToJsonResult(this Controller controller, object target)
    {
        return controller.ToJsonResult(target, null, null, JsonRequestBehavior.DenyGet);
    }

    public static JsonResult ToJsonResult(this Controller controller, object target, string contentType)
    {
        return controller.ToJsonResult(target, contentType, null, JsonRequestBehavior.DenyGet);
    }

    public static JsonResult ToJsonResult(this Controller controller, object target, string contentType, Encoding contentEncoding)
    {
        return controller.ToJsonResult(target, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
    }

    public static JsonResult ToJsonResult(this Controller controller, object target, string contentType, JsonRequestBehavior behavior)
    {
        return controller.ToJsonResult(target, contentType, null, behavior);
    }

In my application, I override the default controller and use the JSON.NET serializer if the type has the DataContract attribute. This functionality is encapsulated in the DataContractJsonResult class, which is not included, but is modeled after the class in the accepted answer to this question.