微软的序列化反序列化组件出来已有好几年了,刚出来的时候各种吐槽。最近在优化代码,比较了一下微软的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. [BUUCTF]REVERSE——[ACTF新生赛2020]easyre

    [ACTF新生赛2020]easyre 附件 步骤 查壳,32位程序,upx壳儿 脱完壳儿,扔进ida 分析 一开始给我们定义了一个数组, v4=[42,70,39,34,78,44,34,40,73 ...

  2. 记一次AWD

    有幸bjx师傅又让我参加了一次awd,算是第二次体验awd,又感觉学习到了很多东西. 第一次打这种模式的时候,我几乎什么都没有做,就给师傅们下载文件,上传文件了.(太菜了) 昨晚分的组,发现没有人是p ...

  3. [BUUCTF]PWN——jarvisoj_level1

    jarvisoj_level1 附件 步骤: 例行检查,32位程序,没有开任何保护 本地运行一下程序,看看大概的情况,可以看到输出了一个地址 32位ida载入,习惯性的检索程序里的字符串,没有发现可以 ...

  4. [BUUCTF]PWN——CmmC_Simplerop

    cmcc_simplerop 附件 步骤 例行检查,32位,开启了nx保护 本地试运行一下程序,查看一下大概的情况 32位ida载入,习惯性的检索程序里的字符串,看了个寂寞,从main函数开始看程序 ...

  5. 预算(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 预算是件重要的事,不然银几一花没边了,那结果可是要牺牺的(以下省略具体描述9^323字) 在Project里做预算,步骤不 ...

  6. CSS的三大特性(继承、层叠和优先级)

    CSS的三大特性(继承.层叠和优先级) 1.css属性的继承 CSS中有些属性是可继承的,何为属性的继承? 一个元素如果没有设置某些属性的值,就会跟随(继承)父元素的属性值.当然,一个元素如果有设置自 ...

  7. 『学了就忘』Linux系统定时任务 — 88、循环执行定时任务

    目录 1.crond服务管理与访问控制 2.crontab命令的访问控制 3.用户级别的crontab命令 4.crontab命令的注意事项 5.系统的crontab设置 (1)/etc/cronta ...

  8. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

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

  9. 【Android开发】问答机器人,聊天类App的开发制作过程记录

    缘起 很久没写项目了,所以单纯的想练练手,正好看到有问答机器人的接口,想到之前也做过聊天项目,为什么不实验一下呢.当然也是简单调用接口的项目,并没有真正的完成问答的算法等等.业余项目,功能不齐全,只实 ...

  10. 【LeetCode】223. Rectangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...