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 更快的序列化的更多相关文章

  1. 使用 ServiceStack.Text 序列化 json 比Json.net更快

    本节将介绍如何使用ServiceStack.Text 来完成高性能序列化和反序列化操作. 在上章构建高性能ASP.NET应用的几点建议 中提到使用高性能类库,有关于JSON序列化的讨论. 在诊断web ...

  2. ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较

    JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...

  3. 使用 ServiceStack.Text 序列化 json的实现代码【转】

    转自:http://www.jb51.net/article/38338.htm 今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft. ...

  4. 使用 ServiceStack.Text 序列化 json

    相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很 ...

  5. 使用 ServiceStack.Text 序列化 json的实现代码

    相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很多 ...

  6. 新型序列化类库MessagePack,比JSON更快、更小的格式

    MessagePack is an efficient binary serialization format. It lets you exchange data among multiple la ...

  7. ServiceStack.Text json中序列化日期格式问题的解决

    标记: ServiceStack.Text,json,序列化,日期 在使用ServiceStack.Text的序列化为json格式的时候,当属性为datetime的时候,返回的是一个new date( ...

  8. 最快的序列化组件protobuf的.net版本protobuf.net

    Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多.     Prot ...

  9. SharePoint 2010中使用SPListItemCollectionPosition更快的结果

    转:http://www.16kan.com/article/detail/318657.html Introduction介绍 In this article we will explore the ...

随机推荐

  1. if..elif语句

    根据用户输入内容打印其权限 # alex --> 超级管理员 # eric --> 普通管理员 # tony,rain --> 业务主管 # 其他 --> 普通用户 name ...

  2. mysql 索引2

    /* 所有MySQL列类型可以被索引.根据存储引擎定义每个表的最大索引数和最大索引长度. 所有存储引擎支持每个表至少16个索引,总索引长度至少为256字节.大多数存储引擎有更高的限制. 索引的存储类型 ...

  3. android file path

    问题 出现的异常为:java.lang.IllegalArgumentException: File /mnt/sdcard/crazyit.bin contains a pathseparator. ...

  4. Mybatis——oracle 的模糊查询 和 日期处理

    第一种 1 <if test="AGTNAM !=null and AGTNAM !=''"> <![CDATA[and AGTNAM like concat(c ...

  5. 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理

    这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...

  6. react.js 公共方法 集合

    截取七牛上传图片的后缀名: export function getInputKey(info){ let self = this; let obj = JSON.parse(info); let qi ...

  7. git初体验(六)git配置

    一.基本配置 看这篇Git 基本配置其实就解决很多问题了. 二.客户端配置 1.git bash 粘贴与编辑 在git bash客户端头部右键,现在属性,然后把"快速编辑模式勾选即可&quo ...

  8. Fiddler Post Debug

    Content-Type: application/json; charset="UTF-8"

  9. 关于IOS显示图片的一些注意事项

    1.-568h 的图片只能在def.png显示,其他的都不支持. 2.在加载图片到Images.xcassets里面的时候,如果图片下面出现Unassigned这个时候这个图片就不能用,得拿到Imag ...

  10. 【BZOJ-3786】星系探索 Splay + DFS序

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 647  Solved: 212[Submit][Status][Discuss] ...