ServiceStack.Text 更快的序列化
Json.net 是以前最经常用的序列化组件,后来又注意到ServiceStack号称最快的,所以我做了以下测试
1)Json.net
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Newtonsoft.Json; namespace Json.net
{
class Program
{
static void Main(string[] args)
{
Order order = new Order()
{
CltAddr = "aaa",
CltName = "Aven",
CltPhone = "0345-3423434"
};
order.OrderId = "001"; Stopwatch sw=new Stopwatch();
sw.Reset();
sw.Start();
for (int i = 0; i < 1000*1000; i++)
{
order.OrderId = i.ToString();
string json = order.ToJson();
}
sw.Stop();
Console.WriteLine("Json.net消耗时间:{0}ms",sw.ElapsedMilliseconds);
Console.Read(); }
} public static class JsonHelper
{
public static String ToJson<T>(this T ojb) where T : class
{
return JsonConvert.SerializeObject(ojb, Formatting.Indented); }
public static T ToInstance<T>(this String jsonStr) where T : class
{
var instance = JsonConvert.DeserializeObject<T>(jsonStr); return instance; }
} class Order
{
public string OrderId { get; set; }
public string CltName;
public string CltPhone;
public string CltAddr;
} }

2)ServiceStack.Text
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ServiceStack; namespace ServiceStace.Text
{
class Program
{
static void Main(string[] args)
{
Order order=new Order();
order.OrderId = "001";
order.CltInfo=new CltInfo()
{
CltAddr ="aaa",
CltName="Aven",
CltPhone = "0345-3423434"
};
Stopwatch sw=new Stopwatch();
sw.Reset();
sw.Start();
for (int i = 0; i < 1000*1000; i++)
{
order.OrderId = i.ToString();
string json = order.ToJson();
}
sw.Stop();
Console.WriteLine("ServiceStack.Text消耗时间:{0}ms",sw.ElapsedMilliseconds);
Console.Read();
}
} class Order
{
public string OrderId { get; set; }
public CltInfo CltInfo { get; set; }
} class CltInfo
{
public string CltName;
public string CltPhone;
public string CltAddr;
}
}

由上图对比可知,在序列化时 serviceStack.Text 比 Json.net 快5倍左右 下面再测试反序列化
但是有一个很致命的缺陷,serviceStack.Text在处理 复杂类就显示很不方便了
如
[Serializable]
public class Order
{
public string OrderId { get; set; }
public ClientInfo CltInfo { get; set; }
}
[Serializable]
public class ClientInfo
{
public string CltName;
public string CltPhone;
public string CltAddr;
}
要序列化 Order 的话,结果只是 一个OrderId:"123223",CltInfo:""
cltInfo是不会被正确序列化的
ServiceStack.Text 更快的序列化的更多相关文章
- 使用 ServiceStack.Text 序列化 json 比Json.net更快
本节将介绍如何使用ServiceStack.Text 来完成高性能序列化和反序列化操作. 在上章构建高性能ASP.NET应用的几点建议 中提到使用高性能类库,有关于JSON序列化的讨论. 在诊断web ...
- ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较
JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...
- 使用 ServiceStack.Text 序列化 json的实现代码【转】
转自:http://www.jb51.net/article/38338.htm 今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft. ...
- 使用 ServiceStack.Text 序列化 json
相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很 ...
- 使用 ServiceStack.Text 序列化 json的实现代码
相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很多 ...
- 新型序列化类库MessagePack,比JSON更快、更小的格式
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple la ...
- ServiceStack.Text json中序列化日期格式问题的解决
标记: ServiceStack.Text,json,序列化,日期 在使用ServiceStack.Text的序列化为json格式的时候,当属性为datetime的时候,返回的是一个new date( ...
- 最快的序列化组件protobuf的.net版本protobuf.net
Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多. Prot ...
- SharePoint 2010中使用SPListItemCollectionPosition更快的结果
转:http://www.16kan.com/article/detail/318657.html Introduction介绍 In this article we will explore the ...
随机推荐
- Change Eclipse Tooltip's Color in Ubuntu
这个问题十分高级,随着Ubuntu版本的变迁这个问题的解决方案也在不断变化 最开始,SystemSettings里面可以设置工具条背景色,后来这个选项在新版本Ubuntu中消失了 我用过Ubuntu1 ...
- Maven的set.xml标签详解
文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...
- js实现登陆页面的拖拽功能
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>登 ...
- 【转】c# 调用windows API(user32.dll)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- JS搞基指南----延迟对象入门提高资料整理
JavaScript的Deferred是比较高大上的东西, 主要的应用还是主ajax的应用, 因为JS和nodeJS这几年的普及, 前端的代码越来越多, 各种回调套回调再套回调实在太让人崩溃, ...
- ASP.NET MVC html help
public static class HtmlHelper { /// <summary> /// 返回没有边框的只读的TextBox标签 /// </summary> // ...
- Python 素数判断;以及默尼森数
1. 素数/质数 只能被2或者本身整除的正整数. 2. 默尼森数 P是素数且M也是素数,并且满足等式M=2^P-1,则称M为默尼森数. 编程小要求: 输出前5个默尼森数 1)最外层循环找素数 中间层循 ...
- [bzoj4552][Tjoi2016][Heoi2016]排序
Description 给出一个$1$到$n$的全排列,现在对这个全排列序列进行$m$次局部排序,排序分为$2$种: $1.(0,l,r)$表示将区间$[l,r]$的数字升序排序; $2.(1,l,r ...
- js实现网页瀑布流布局
效果图: html代码实现网页布局: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- poj 2653 线段相交
题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...