微软的序列化反序列化组件出来已有好几年了,刚出来的时候各种吐槽。最近在优化代码,比较了一下微软的Serialize和Newtonsoft的SerializeObject,感觉大部分场景下可以用微软的序列化组件了,Newtonsoft第三方可能被我放弃掉。测试有交换顺序,也有多次测试。当然如果加上JsonSerializerOptions,而且全部配置起来性能就会有所下降,毕竟这么多配置在这呢,但是这样也会更加灵活。

初步结论,在使用微软默认配置下数据量大序列化速度比Newtonsoft快了一倍。

 1 using Newtonsoft.Json;
2 using System;
3 using System.Diagnostics;
4 namespace JsonTest
5 {
6 internal class Program
7 {
8 static void Main(string[] args)
9 {
10 var count = 10_000;
11 var elapsedMilliseconds = Serialize(count, () =>
12 {
13 JsonConvert.SerializeObject(new WeatherForecast
14 {
15 Date = DateTime.Now,
16 Summary = "Hot",
17 TemperatureCelsius = 88
18 });
19 });
20 Console.WriteLine($"serialize object count:{count}, newtonsoft used: {elapsedMilliseconds} seconds");
21 elapsedMilliseconds = Serialize(count, () =>
22 {
23 System.Text.Json.JsonSerializer.Serialize(new WeatherForecast
24 {
25 Date = DateTime.Now,
26 Summary = "Hot",
27 TemperatureCelsius = 88
28 });
29 });
30 Console.WriteLine($"serialize object count:{count}, textjson used : {elapsedMilliseconds} seconds");
31 Console.WriteLine("***************************************************");
32 count = 10_000_000;
33 elapsedMilliseconds = Serialize(count, () =>
34 {
35 JsonConvert.SerializeObject(new WeatherForecast
36 {
37 Date = DateTime.Now,
38 Summary = "Hot",
39 TemperatureCelsius = 88
40 });
41 });
42 Console.WriteLine($"serialize object count:{count}, newtonsoft used: {elapsedMilliseconds} seconds");
43 elapsedMilliseconds = Serialize(count, () =>
44 {
45 System.Text.Json.JsonSerializer.Serialize(new WeatherForecast
46 {
47 Date = DateTime.Now,
48 Summary = "Hot",
49 TemperatureCelsius = 88
50 });
51 });
52 Console.WriteLine($"serialize object count:{count}, textjson used : {elapsedMilliseconds} seconds");
53 Console.ReadKey();
54 /*
55 serialize object count:10000, newtonsoft used: 288 seconds
56 serialize object count:10000, textjson used : 45 seconds
57 ***************************************************
58 serialize object count:10000000, newtonsoft used: 10324 seconds
59 serialize object count:10000000, textjson used : 5681 seconds
60 */
61 }
62
63 static long Serialize(int count,Action action)
64 {
65 Stopwatch stopwatch = Stopwatch.StartNew();
66 for(int i = count; i > 0; i--)
67 {
68 action();
69 }
70 stopwatch.Stop();
71 var result = stopwatch.ElapsedMilliseconds;
72 stopwatch.Reset();
73 return result;
74 }
75 }
76 internal class WeatherForecast
77 {
78 public DateTimeOffset Date { get; set; }
79 public int TemperatureCelsius { get; set; }
80 public string Summary { get; set; }
81 }
82 }

微软文档里面有各种介绍,不再详述!

从 Newtonsoft.Json 迁移到 System.Text.Json - .NET | Microsoft Docs

微软的Serialize和Newtonsoft的SerializeObject比较的更多相关文章

  1. 自定义$('#form').serialize() var params = $('#xxx_form').serializeObject();

    //注意:获取之前 $("#id").removeAttr("disabled"); $.fn.serializeObject = function () { ...

  2. Azure 元数据服务:适用于 Windows VM 的计划事件(预览)

    计划事件是 Azure 元数据服务中的其中一个子服务. 它负责显示有关即将发生的事件(例如,重新启动)的信息,使应用程序可以为其做准备并限制中断. 它可用于所有 Azure 虚拟机类型(包括 PaaS ...

  3. 微软自带的Serialization和Newtonsoft简单测试

    刚刚对这两个进行了一下小小的测试 发现 当转换的内容少的时候  微软自带的比Newtonsoft速度要快一些,内容多的时候反之,当内容多到一定量的时候微软自带的就不能转换了,需要修改一下MaxJson ...

  4. C#.NET序列化XML、JSON、二进制微软自带DLL与newtonsoft(json.net)

    序列化是将对象转换成另一种格式(XML.json.二进制byte[]) JSON序列化 .NET中有三种常用的JSON序列化的类,分别是: Newtonsoft.Json.JsonConvert类(推 ...

  5. In .net 4.8,calculate the time cost of serialization in BinaryFormatter,NewtonSoft.json,and System.Text.Json.JsonSerializer.Serialize

    using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; us ...

  6. C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize

    In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...

  7. Newtonsoft.Json序列化和反序列之javascriptConvert.SerializeObject,DeserializeObject,JsonWriter,JsonReader

    这里下载:http://www.newtonsoft.com/products/json/安装:   1.解压下载文件,得到Newtonsoft.Json.dll   2.在项目中添加引用.. jav ...

  8. @Html.Raw() 与Newtonsoft.Json.JsonConvert.SerializeObject()

    一.后台 ViewBag.TypeList = typeList; 二.前台C# @{     var typeListFirst = ViewBag.TypeList;} 三.前台js中 var t ...

  9. .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)

    1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/

随机推荐

  1. 模糊C均值算法

    Fuzzy C-Means读书笔记 一.算法简介 很显然,图中的数据集可分为两个簇.借鉴K-Means算法的思想,利用单个特殊的点(质心)表示一个簇.因此,我们用\(C_1\)和\(C_2\)分别表示 ...

  2. .NET Core工程应用系列(1) 定制化Audit.NET实现自定义AuditTarget

    需求背景 最近在项目上需要增加对用户操作进行审计日志记录的功能,调研了一圈,在.net core生态里,用的最多的是Audit.NET.浏览完这个库的文档后,觉得大致能满足我们的诉求,于是建立一个控制 ...

  3. 13 - Vue3 UI Framework - 完善官网

    为了提升用户体验,今天我们来对 jeremy-ui 官网做一个优化 返回阅读列表点击 这里 Markdown 解析支持 ️ 习惯用 markdown 语法编辑,所以我们增加项目源码对 markdown ...

  4. 优雅的按键模块-----Multi-button

    优雅的按键模块-----Multi-button ​ 在我们日常开发和使用的过程中常常使用了一些按键,利用按键实现不同的功能,比如长按,短按,双击等等.但是每次都是采用标志等等来实现信息的读取,是否有 ...

  5. SQL注入绕过

    空格字符绕过 %09 TAB建(水平) %0a 新建一行,换行 %0b TAB建(垂直) %0c 新的一页 %0d return 功能 %a0 空格 %00 /**/ /*!*/./*!50009*/ ...

  6. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  7. 【LeetCode】243. Shortest Word Distance 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 【LeetCode】661. Image Smoother 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...

  9. 计算机视觉1->opencv4学习指南1 | 环境配置与例程

    opencv虽然很有名,但是自己一直没怎么玩过,暑假的时候使用深度相机做项目,但负责的不是代码模块,也只是配好了环境,没有继续了解图像处理.最近电子实习老师有教这个东西,但是身边不少同学遇到了麻烦,所 ...

  10. MySQL中写操作

    具体到操作流程: 当执行某个写操作的 SQL 时,引擎将这行数据更新到内存的同时把对应的操作记录到 redo log 里面,然后处于 prepare 状态.并把完成信息告知给执行器. 执行器生成对应操 ...