c# 序列化效率比拼

摘要:
JsonConvert。SerializeObject;;sp_脚本。停止();安慰写入行;#Endregion#regionJil序列化并检索数据输出。ToString();sp_脚本。Restart();使用{JSON.Serialize;}sp_脚本。停止();安慰写入行;#Endregion}//测试模拟数据源的默认长度参数是10wpublicstaticdynamicGetData{varlstRes=newList<dynamic>();对于{vardata=new{Name=“Zhang San”+i,Age=20,IsChild=i%5==0?特别是Jil更明显。将两个请求更改为一个,减少了中间进程的序列化和反序列化。

前言:作为开发人员,对象的序列化经常用到,特别是在现在前后端分离 采用json 交互 ,就将原来用过的几种方式总结了下,也算是做一个记录,顺便做了下性能测试。

1:内置 JavaScriptSerializer

2:常用  Newtonsoft.Json.dll

3:第3方 Jil  https://github.com/kevin-montrose/Jil

using Jil;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Web.Script.Serialization;

namespace csDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var sp_script = new Stopwatch();
            //模拟数据源 默认10w
            var dataLsit = GetData();

            #region  JavaScriptSerializer 添加 System.Web.Extensions引用
            sp_script.Start();//开始计数
            var js = new JavaScriptSerializer
            {
                MaxJsonLength = Int32.MaxValue  //设置为int的最大值 
            };
            js.Serialize(dataLsit);
            sp_script.Stop();
            Console.WriteLine("JavaScriptSerializer序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
            #endregion

            #region  Newtonsoft.Json.dll 
            sp_script.Restart();//停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。
            JsonConvert.SerializeObject(dataLsit); ;
            sp_script.Stop();
            Console.WriteLine("Newtonsoft 序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
            #endregion

            #region Jil 序列化 取出数据  output.ToString(); 
            sp_script.Restart();
            using (var output = new StringWriter())
            {
                 JSON.Serialize(dataLsit,output );
               
            }
            sp_script.Stop();
            Console.WriteLine("Jil 序列化方式序列化" + dataLsit.Count + "个对象耗时:" + sp_script.ElapsedMilliseconds + "毫秒");
            #endregion
        }

        // 测试模拟数据源 length 默认参数10w 
        public static dynamic GetData(int length=100000)
        {
            var lstRes = new List<dynamic>();
            for (var i = 0; i < length; i++)
            {
                var data = new 
                {
                    Name = "张三" + i,
                    Age = 20,
                    IsChild = i % 5 == 0 ? true : false,
                    Test1 = DateTime.Now,
                    Test2 = i.ToString(),
                    Test3 = i.ToString(),
                    Test4 = i.ToString(),
                    Test5 = i.ToString(),
                    Test6 = i.ToString(),
                    Test7 = i.ToString(),
                    Test8 = i.ToString(),
                    Test9 = i.ToString(),
                    Test10 = i.ToString()
                };

                lstRes.Add(data);
            }
            return lstRes;
        }
    }

c# 序列化效率比拼第1张

总结:使用过程中发现GetData方法 使用dynamic (开始使用实体类) 效率反而快一点。 特别是 Jil 更明显。

Jil 学习地址:

https://github.com/kevin-montrose/Jil

https://www.dotnetjalps.com/2015/10/convert-c-object-into-json-and-vice.html

附加 webapi  :使用Jil提升Json序列化性能

https://blog.csdn.net/sqqyq/article/details/51692342

 注意:jil 对序列化类型有要求:(如果是object 或者是dataset 不支持) 时间需要自定义 建议时间数据转换string 默认的格式 yyyy-MM-dd hh:mm:ss

Supported Types

Jil will only (de)serialize types that can be reasonably represented as JSON.

The following types (and any user defined types composed of them) are supported:

  • Strings (including char)
  • Booleans
  • Integer numbers (int, long, byte, etc.)
  • Floating point numbers (float, double, and decimal)
  • DateTimes & DateTimeOffsets
    • Note that DateTimes are converted to UTC time to allow for round-tripping, use DateTimeOffsets if you need to preserve timezone information
    • See Configuration for further details
  • TimeSpans
    • See Configuration for further details
  • Nullable types
  • Enumerations
    • Including [Flags]
  • Guids
  • IList<T>, ICollection<T>, and IReadOnlyList<T> implementations
  • IDictionary<TKey, TValue> implementations where TKey is a string or enumeration
  • ISet<T>

额外笔记:

css 学习网址 :https://www.zhangxinxu.com/wordpress/2012/09/css3-3d-transform-perspective-animate-transition/

js:http://www.ruanyifeng.com/blog/archives.html

工作记录:

页面导入exel 建议使用 js-xlsx 

 )  提交到后台在逻辑判断  。2次请求改成一次请求,减少中间过程序列化反序列。

 js-xlsx:https://www.cnblogs.com/liuxianan/p/js-excel.html

免责声明:文章转载自《c# 序列化效率比拼》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue.js 源码分析(二十八) 高级应用 transition组件 详解Linux清理磁盘空间下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

深入 js 深拷贝对象

前言 对象是 JS 中基本类型之一,而且和原型链、数组等知识息息相关。不管是面试中,还是实际开发中我们都会碰见深拷贝对象的问题。 顾名思义,深拷贝就是完完整整的将一个对象从内存中拷贝一份出来。所以无论用什么办法,必然绕不开开辟一块新的内存空间。 通常有下面两种方法实现深拷贝: 迭代递归法 序列化反序列化法 我们会基于一个测试用例对常用的实现方法进行...

C# Xml序列化与反序列化

Xml文本的序列化与反序列化: public static class XmlSerializeHelper { // 序列化:对象 -> Xml文本 public static string SerializeToXmlString(object obj) {...

DICOM文件添加私有Tag(DCMTK Private Tag)

                                                                                         DICOM文件插入私有tag   在处理dicom文件过程中,往往需要插入自定义的tag,并保存为dicom文件。在网上查资料,都比较少,经过一番探索,有点收获。与大家分享,希望能...

Spring Data Redis入门示例:数据序列化 (四)

概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行,但当出现数据错误,对数据进行排查时,就无从下手了。 序列化器 在Spring Data Redis中,用户自定义类型和存储数据之间的转换(反之亦然)由org...

Kafka:生产者

Kafka java客户端数据生产流程解析 ProducerRecord ProducerRecord 含义: 发送给Kafka Broker的key/value 值对 //ProducerRecord的成员变量 public class ProducerRecord<K, V> { private final String top...

ASP.NET 中实现会话状态的基础

简介 在 Web 应用程序这样的无状态环境中,了解会话状态的概念并没有实际的意义。尽管如此,有效的状态管理对于大多数 Web 应用程序来说都是一个必备的功能。Microsoft® ASP.NET 以及许多其他服务器端编程环境都提供了一个抽象层,允许应用程序基于每个用户和每个应用程序存储持久性数据。 需要特别注意的是,Web 应用程序的会话状态是应用程序在不...