Asp.net Web Api开发(第二篇)性能:使用Jil提升Json序列化性能
2016-06-16 16:40
633 查看
看了几篇网上关于各种序列化工具的性能对比,在这里再粘贴下:
我们使用了ASP.NET WEB API来提供RESTfull风格的接口给APP调用,默认序列化库用的是:Newtonsoft.Json
为了进一步提高服务端的性能,有必要将序列化库进行替换。从上图可以看出,Jil是最快的Json序列化库了。为了验证其性能,我们也写了相关代码,将Jil和Newtonsoft.Json进行的比较。确实Jil的性能要优越不少。于是决定就用Jil来替换了。
第一步:引用Jil.dll,同目录下也要有Sigil.dll,Jil是基于Sigil开发的。
第二部:编写一个JilFormatter:
我们使用了ASP.NET WEB API来提供RESTfull风格的接口给APP调用,默认序列化库用的是:Newtonsoft.Json
为了进一步提高服务端的性能,有必要将序列化库进行替换。从上图可以看出,Jil是最快的Json序列化库了。为了验证其性能,我们也写了相关代码,将Jil和Newtonsoft.Json进行的比较。确实Jil的性能要优越不少。于是决定就用Jil来替换了。
第一步:引用Jil.dll,同目录下也要有Sigil.dll,Jil是基于Sigil开发的。
第二部:编写一个JilFormatter:
using Jil; using System; using System.IO; using System.Net; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace RRPService.WebApi.Comm { public class JilFormatter: MediaTypeFormatter { private readonly Options mJilOptions; /// <summary> /// Jil Json序列化器 /// </summary> public JilFormatter() { mJilOptions = new Options( dateFormat: DateTimeFormat.MillisecondsSinceUnixEpoch, excludeNulls:true, includeInherited: true, serializationNameFormat: SerializationNameFormat.CamelCase); SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)); SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true)); } public override bool CanReadType(Type type) { if (type == null) { throw new ArgumentNullException("type"); } return true; } public override bool CanWriteType(Type type) { if (type == null) { throw new ArgumentNullException("type"); } return true; } public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger) { return Task.FromResult(this.DeserializeFromStream(type, readStream)); } private object DeserializeFromStream(Type type, Stream readStream) { try { using (var reader = new StreamReader(readStream)) { MethodInfo method = typeof(JSON).GetMethod("Deserialize", new Type[] { typeof(TextReader), typeof(Options) }); MethodInfo generic = method.MakeGenericMethod(type); return generic.Invoke(this, new object[] { reader, mJilOptions }); } } catch { return null; } } public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext) { var streamWriter = new StreamWriter(writeStream); JSON.Serialize(value, streamWriter, mJilOptions); streamWriter.Flush(); return Task.FromResult(writeStream); } } }替换默认Json序列化库:
using RRPService.WebApi.Comm; using System.Web.Http; using System.Web.Mvc; namespace RRPService.WebApi.App { /// <summary> /// web api 服务 /// </summary> public class WebApiApplication : System.Web.HttpApplication { /// <summary> /// 服务启动 /// </summary> protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); HttpConfiguration fConfig = GlobalConfiguration.Configuration; fConfig.Formatters.Remove(fConfig.Formatters.JsonFormatter); fConfig.Formatters.Insert(0, new JilFormatter()); } } }
相关文章推荐
- 使用 Python 处理 JSON 格式的数据
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- C#自定义序列化ISerializable的实现方法
- C# XML序列化方法及常用特性总结分析
- c#数据的序列化和反序列化(推荐版)
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C# JSON格式化转换辅助类 ConvertJson
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧