Replace JSON.NET with Jil JSON serializer in ASP.NET Web API
I have recently come across a comparison of fast JSON serializers in .NET, which shows that Jil JSON serializer is one of the fastest.
Jil is created by Kevin Montrose developer at StackOverlow and it is apparently heavily used by Stackoveflow.
This is only one of many benchmarks you can find on Github project website.

You can find more benchmarks and the source code at this locationhttps://github.com/kevin-montrose/Jil
In this short article I will cover how to replace default JSON serializer in Web API with Jil.
Create Jil MediaTypeFormatter
First, you need to grab Jil from NuGet
PM> Install-Package Jil
After that, create JilFormatter using code below.
public class JilFormatter : MediaTypeFormatter
{
private readonly Options _jilOptions;
public JilFormatter()
{
_jilOptions=new Options(dateFormat:DateTimeFormat.ISO8601);
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)
{
var task= Task<object>.Factory.StartNew(() => (this.DeserializeFromStream(type,readStream)));
return task;
} 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, _jilOptions});
}
}
catch
{
return null;
} } public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
{
using (TextWriter streamWriter = new StreamWriter(writeStream))
{
JSON.Serialize(value, streamWriter, _jilOptions);
var task = Task.Factory.StartNew(() => writeStream);
return task;
}
}
}
This code uses reflection for deserialization of JSON.
Replace default JSON serializer
In the end, we need to remove default JSON serializer.
Place this code at beginning of WebApiConfig
config.Formatters.RemoveAt();
config.Formatters.Insert(, new JilFormatter());
Replace JSON.NET with Jil JSON serializer in ASP.NET Web API的更多相关文章
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- ASP.NET Web API中的JSON和XML序列化
ASP.NET Web API中的JSON和XML序列化 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok ...
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
- 使用Jil序列化JSON提升Asp.net web api 性能
JSON序列化无疑是Asp.net web api 里面性能提升最重要的一环. 在Asp.net web api 里面我们可以插入自定义的MediaTypeFormatter(媒体格式化器), 说白了 ...
- Replace JSON.NET with ServiceStack.Text in ASP.NET Web API
Because ServiceStack.Text performs much better I recently stumbled across a comparison of JSON seria ...
- ASP.NET Web Api返回对象类型为JSON还是XML
在Umbraco平台上开发过程中,我用WebApi返回JSON result给前端 前端使用React调用这个web api来获取JSON result 我写的web api方法是返回JSON 类型的 ...
- On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
Ints are easy. Strings are mostly easy. Dates? A nightmare. They always will be. There's different c ...
- ASP.NET Web API 2.1支持Binary JSON(Bson)
ASP.NET Web API 2.1内建支持XML.Json.Bson.form-urlencoded的MiME type,今天重点介绍下Bson.BSON是由10gen开发的一个数据格式,目前主要 ...
- ASP.NET Web API 如何通过程序控制返回xml还是json
雖然 ASP.NET Web API 內建支援 JSON 與 XML 兩種輸出格式,並依據瀏覽器端送出的 Accept 標頭自動決定回應的內容格式,不過有時候我們的確也需要讓程式來控制要回應哪種格式, ...
随机推荐
- python中文输出和写入文本
中文输出 #-*-coding:utf8-*- import requests import re timeout = 8 headers = {'User-Agent':'Mozilla/5.0 ( ...
- JSON 序列化和反序列化——JavaScriptSerializer实现
一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...
- 【转】asp.net导出数据到Excel的三种方法
来源:http://www.cnblogs.com/lishengpeng1982/archive/2008/04/03/1135490.html 原文出处:http://blog.csdn.net/ ...
- OpenJudge计算概论-鸡尾酒疗法
/*===================================== 鸡尾酒疗法 总时间限制: 1000ms 内存限制: 65536kB 描述 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HA ...
- 【转】Deprecated: Function ereg_replace() is deprecated的解决方法
这个问题是因为你用的php版本过高. 在php5.3中,正则函数ereg_replace已经废弃,而dedecms还继续用.有两个方案可以解决以上问题: 1.把php版本换到v5.3下. 2.继续使用 ...
- sqlplus将查询结果重定向到文件,不输出到屏幕
--每行的字符数目 --该参数设置每页输出的行数.n=0表示不产生新页 --显示和拷贝long类型值的最大宽度的设置,最大值2G --sqlplus检索long类型值的增量大小.由于内存的限制 可按增 ...
- (转帖)BootStrap入门教程 (一)
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MAR ...
- IRedisClient 常用方法说明
事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...
- Eclipse中添加web dynamic project
因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接: ...
- 拿nodejs快速搭建简单Oauth认证和restful API server攻略
拿nodejs快速搭建简单Oauth认证和restful API server攻略:http://blog.csdn.net/zhaoweitco/article/details/21708955 最 ...