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. webpack入坑之旅(二)loader入门

    这是一系列文章,此系列所有的练习都存在了我的github仓库中vue-webpack 在本人有了新的理解与认识之后,会对文章有不定时的更正与更新.下面是目前完成的列表: webpack入坑之旅(一)不 ...

  2. auto refresh iframe

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. Maven的set.xml标签详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  4. __getattribute__

    class Foo: def __init__(self,x): self.x = x def __getattribute__(self, item): print('不管是否纯在,我都会执行') ...

  5. js打字机效果实现

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>打 ...

  6. ORA-600(qerltcInsertSelectRop_bad_state)错误

    来源于: http://blog.itpub.net/22458783/viewspace-615501/ 这是碰到的第一个11.2上的bug,在利用IGNORE_ROW_ON_DUPKEY_INDE ...

  7. 哈希 poj 3274

    n个牛 二进制最多k位 给你n个数 求max(j-i)&&对应二进制位的和相同 7    1  1  1  倒的 6    0  1  1 7    1  1  1 2    0  1 ...

  8. 浏览器XMLHttpRequest案例

    /* Cross-Browser XMLHttpRequest v1.2 ================================= Emulate Gecko 'XMLHttpRequest ...

  9. Servlet、Filter和Listener

    Java Servlet是与平台无关的服务器端组件,运行于Servlet容器中(如Tomcat),Servlet容器负责Servlet和客户端的通信以及调用Servlet的方法,Servlet和客户端 ...

  10. 多线程中的synchronized

    synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码 ...