首页 > 代码库 > 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具

使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具

ASP.Net MVC默认的JSON序列化使用的是微软自己的JavaScriptSerializer。性能低不说,最让人受不了的是Dictionary<,>和Hashtable类型居然对应的json是[{"Key":"a","Value":1}]而不是{"a":1}。真是奇葩到家了,跟前端根本没法集成!

决定还是用JSON.Net来做吧!查了各种资料,网上的要么代码太复杂,要么没法完全实现。又在ILSpy分析了MVC源码,然后调试了半天,终于有了初步的解决办法:

1、反序列化,首先建立下面的类:

 1 using System;
 2 using System.Globalization;
 3 using System.IO;
 4 using System.Web.Mvc;
 5 using Newtonsoft.Json.Linq;
 6 
 7 namespace HZ
 8 {
 9     public class JsonNetValueProviderFactory : ValueProviderFactory
10     {
11         public override IValueProvider GetValueProvider(ControllerContext ctlContext)
12         {
13             //if (!controllerContext.HttpContext.Request.ContentType.
14             //    StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
15             //{
16             //    return null;
17             //}
18 
19             var reader = new StreamReader(ctlContext.HttpContext.Request.InputStream);
20             reader.BaseStream.Position = 0;
21             var json = reader.ReadToEnd();
22             if (string.IsNullOrEmpty(json))
23                 return null;
24 
25             var jtoken = json.StartsWith("[") 
26                 ? JArray.Parse(json) as JContainer
27                 : JObject.Parse(json) as JContainer;
28             return new JsonNetValueProvider(jtoken);
29         }
30     }
31 
32     public class JsonNetValueProvider : IValueProvider
33     {
34         private JContainer _jValue;
35 
36         public JsonNetValueProvider(JContainer jval)
37         {
38             _jValue =http://www.mamicode.com/ jval;
39         }
40 
41         public bool ContainsPrefix(string prefix)
42         {
43             return true;
44         }
45 
46         public ValueProviderResult GetValue(string key)
47         {
48             var jtoken = _jValue.SelectToken(key);
49             if (jtoken == null)
50             {
51                 jtoken = _jValue;
52             }
53             return new JsonNetValueProviderResult(jtoken, key, null);
54         }
55     }
56 
57     public class JsonNetValueProviderResult : ValueProviderResult
58     {
59         private JToken _jtoken;
60         public JsonNetValueProviderResult(JToken valueRaw, string key, CultureInfo info)
61         {
62             _jtoken = valueRaw;
63         }
64         public override object ConvertTo(Type type, CultureInfo culture)
65         {
66             return _jtoken?.ToObject(type);
67         }
68     }
69 
70     public class JsonNetModelBinder : DefaultModelBinder
71     {
72         public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
73         {
74             var provider = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
75             return provider.ConvertTo(bindingContext.ModelType);
76         }
77     }
78 }

2、然后再Application_Start或者Router初始化时调用下面代码:

            //重置Json序列化方式,改用JSON.Net
            ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.
                OfType<JsonValueProviderFactory>().FirstOrDefault());
            ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());

            //重置系统的Binder,使Dictionary可以正常json
            ModelBinders.Binders.DefaultBinder = new JsonNetModelBinder();

原理基本上就是替换系统自带的ValueProviderFactory和DefaultModelBinder,然后改用自己的类来调用JSON.Net实现。

3、序列化的过程比较简单,建立一个JsonNetResult类,然后mvc方法返回这个类型就可以了。

 public class JsonNetResult : JsonResult
    {
        public JsonSerializerSettings Settings { get; private set; }
        public override void ExecuteResult(ControllerContext context)
        {
            //if (context == null)
            //    throw new ArgumentNullException("context");
            //if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            //    throw new InvalidOperationException("JSON GET is not allowed");

            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

            if (this.ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;
            if (this.Data =http://www.mamicode.com/= null)
                return;

            var scriptSerializer = JsonSerializer.Create(this.Settings);

            using (var sw = new StringWriter())
            {
                scriptSerializer.Serialize(sw, this.Data);
                response.Write(sw.ToString());
            }
        }
    }

 

使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具