微软的Serialize和Newtonsoft的SerializeObject比较
微软的序列化反序列化组件出来已有好几年了,刚出来的时候各种吐槽。最近在优化代码,比较了一下微软的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比较的更多相关文章
- 自定义$('#form').serialize() var params = $('#xxx_form').serializeObject();
//注意:获取之前 $("#id").removeAttr("disabled"); $.fn.serializeObject = function () { ...
- Azure 元数据服务:适用于 Windows VM 的计划事件(预览)
计划事件是 Azure 元数据服务中的其中一个子服务. 它负责显示有关即将发生的事件(例如,重新启动)的信息,使应用程序可以为其做准备并限制中断. 它可用于所有 Azure 虚拟机类型(包括 PaaS ...
- 微软自带的Serialization和Newtonsoft简单测试
刚刚对这两个进行了一下小小的测试 发现 当转换的内容少的时候 微软自带的比Newtonsoft速度要快一些,内容多的时候反之,当内容多到一定量的时候微软自带的就不能转换了,需要修改一下MaxJson ...
- C#.NET序列化XML、JSON、二进制微软自带DLL与newtonsoft(json.net)
序列化是将对象转换成另一种格式(XML.json.二进制byte[]) JSON序列化 .NET中有三种常用的JSON序列化的类,分别是: Newtonsoft.Json.JsonConvert类(推 ...
- 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 ...
- 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 ...
- Newtonsoft.Json序列化和反序列之javascriptConvert.SerializeObject,DeserializeObject,JsonWriter,JsonReader
这里下载:http://www.newtonsoft.com/products/json/安装: 1.解压下载文件,得到Newtonsoft.Json.dll 2.在项目中添加引用.. jav ...
- @Html.Raw() 与Newtonsoft.Json.JsonConvert.SerializeObject()
一.后台 ViewBag.TypeList = typeList; 二.前台C# @{ var typeListFirst = ViewBag.TypeList;} 三.前台js中 var t ...
- .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)
1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/
随机推荐
- iOS 实现简单的界面切换
以下是在iOS中最简单的界面切换示例.使用了多个Controller,并演示Controller之间在切换界面时的代码处理. 实现的应用界面: 首先,创建一个window-based applicat ...
- SpringCloud Alibaba实战(12:引入Dubbo实现RPC调用)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 大家好,我是老三,断更了半年,我又滚回来继续写这个系列了,还有人看吗-- 在前面的章 ...
- webservice注意事项
1.private static final QName PORT_NAME = new QName("http://server.helloworld.cxf.demo/",&q ...
- MySQLs数据库建外键时自动跑到缩影处,真奇怪
MySQLs数据库建外键时自动跑到缩影处,真奇怪MyISAM引擎不支持外键:InnoDB存储引擎支持外键.如何解决的,把表修改成innodb类型吧用的工具是SQLyog Ultimate如图所示:
- 当是class com.cosl.po.Pc$$EnhancerByCGLIB$$38c58f03时,反射属性都他妈不好用了
当是class com.cosl.po.Pc$$EnhancerByCGLIB$$38c58f03时,反射属性都他妈不好用了 搞不懂为什么?
- 【LeetCode】1101. The Earliest Moment When Everyone Become Friends 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- STC8A,STC8G,STC8H系列的IRC内部振荡源频率调节
从STC15开始, 宏晶就在内置RC震荡源(内置时脉, 宏晶称之为IRC)这条路上越走越远. STC15这一代仅仅是"有", 精度和漂移差强人意. 从STC8开始对IRC的调节就越 ...
- EMA
目录 源 设置 结果 源 Exponential moving average (EMA) 是一个非常有用的trick, 起到加速训练的作用. 近来发现, 该技巧还可以用于提高网络鲁棒性(约1% ~ ...
- uniapp以及微信小程序中scroll-view隐藏滚动条 自定义滚动条
隐藏滚动条 1.全局隐藏滚动条,在app.vue中 ::-webkit-scrollbar{ display: none; } 2.局部隐藏藏滚动条 样式没有使用scoped属性时, 否则无效. .u ...
- CS5210完全替代AG6202|HDMI转VGA不带音频输出的芯片+原理图|替代兼容AG6202
CS5210完全替代AG6202|HDMI转VGA不带音频输出的芯片+原理图|替代兼容AG6202 安格AG6202是一个HDMI转VGA不带音频解决方案,用于实现HDMI1.4高分辨率视频转VGA转 ...