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. svn1.8 server client eclipse 插件 配置 完全教程

    svn毋庸置疑,广受欢迎的版本管理软件,我们这里以1.8.10版本为例 本文分三部分 第一部分,服务器端svn安装与配置 第二部分,eclipse下svn插件安装与配置 第三部分,客户端svn简单介绍 ...

  2. Linux中sudo配置

    Linux下的sudo及其配置文件/etc/sudoers的详细配置. 1.sudo介绍 sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的ro ...

  3. 第八章:Java集合

    1.Java集合 A:对象的容器. B:实现数据结构(栈.队列) 2.  Set:无序不重复 List: 有序可重复,长度可变. Map: 存放键值对. 3.  Iterator foreach

  4. Hibernate原生SQL映射MySQL的CHAR(n)类型到String时出错

    今天在用Hibernate通过原生SQL和ResultTransformer映射时,出现数据类型不匹配的错误.但是通过Entity映射,没有问题.在网上找了好多答案,终于解决了. 核心代码: Stri ...

  5. IntelliJ_13_配置tomcat

    一.下载tomcat7并解压 http://tomcat.apache.org/download-70.cgi http://apache.fayea.com/tomcat/tomcat-7/v7.0 ...

  6. mysql之路【第三篇】

    1,查看表的结构 desc   表名; 查看表的详细结构 show   create  table; show   create  table   \G;   (加上G格式化输出), 2,修改表 2. ...

  7. c#学习<一> 基础知识

    http://www.25hoursaday.com/CsharpVsJava.html ECMA-334 关键字 累计103 个,其中关键字77个,上下文关键字26个.(c#5.0) 标识符 1. ...

  8. 如何在HTMl网页中插入百度地图

    方法/步骤 1.打开"百度地图生成器"的网址:http://api.map.baidu.com/lbsapi/creatmap/index.html 如下图: 2.在"1 ...

  9. SQLite数据库的基本操作

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...

  10. 网络爬虫2--PHP/CURL库(client URL Request Library)

    PHP/CURL库功能   多种传输协议.CURL(client URL Request Library),含义是“客户端URL请求库”. 不像上一篇所用的PHP内置网络函数,PHP/CURL支持多种 ...