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. MapReduce编程示例

    1.将hadoop插件放入eclipse/plugins目录中 2.eclipse配置hadoop 依赖包目录 Window—Preferences 3.新建Map/Reduce Project项目 ...

  2. Beta项目冲刺 --第一天

    新的开始.. 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽图 3.冲刺 ...

  3. 创建Maven项目

    在MyEclipse10中创建Maven Web项目 1.构建maven项目 2.将maven项目转换成Dynamic Web Project 3.设置部署集 4.pom.xml文件配置 参考: ht ...

  4. Chrome 自动填充的表单是淡黄色的背景

    Chrome 自动填充的表单是淡黄色的背景解决方案; input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #fff inset; - ...

  5. android 之fragment创建

    1.使用xml标签 1.1定义两个重要属性  <fragment         android:id="@+id/fregment_top"        android: ...

  6. Nginx配置文件详细说明

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等worker_process ...

  7. [转] 使用Spring MVC构建REST风格WEB应用

    原文地址:http://fancy888.iteye.com/blog/1629120 对于运行在网络上的MIS系统而言,处理数据的是整个系统的主要任务,翻开程序我们可以看到,80%以上的代码都在处理 ...

  8. 我的第一个jsp程序-实现注册登录留言功能

    1,注册功能,包括两个页面 zhuce.jsp注册页面 <%@ page language="java" contentType="text/html; chars ...

  9. Laplacian算子

    多元函数的二阶导数又称为Laplacian算子: \[ \triangledown f(x, y) = \frac {\partial^2 f}{\partial x^2} + \frac {\par ...

  10. 强连通 HDU3072

    n个点m条边 m条边 权值 简单点说就是求把所有强连通分量连在一起所需的最小花费 不用双向 图是联通的  cost[] 维护到这里的最小花费求和 #include<stdio.h> #in ...