微軟的Serialize和Newtonsoft的SerializeObject比較

星仔007發表於2022-01-13

微軟的序列化反序列化元件出來已有好幾年了,剛出來的時候各種吐槽。最近在優化程式碼,比較了一下微軟的Serialize和Newtonsoft的SerializeObject,感覺大部分場景下可以用微軟的序列化元件了,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 
22             elapsedMilliseconds = Serialize(count, () =>
23             {
24                 System.Text.Json.JsonSerializer.Serialize(new WeatherForecast
25                 {
26                     Date = DateTime.Now,
27                     Summary = "Hot",
28                     TemperatureCelsius = 88
29                 });
30             });
31             Console.WriteLine($"serialize object count:{count}, textjson used : {elapsedMilliseconds} seconds");
32 
33             Console.WriteLine("***************************************************");
34 
35             count = 10_000_000;
36             elapsedMilliseconds = Serialize(count, () =>
37            {
38                JsonConvert.SerializeObject(new WeatherForecast
39                {
40                    Date = DateTime.Now,
41                    Summary = "Hot",
42                    TemperatureCelsius = 88
43                });
44            });
45             Console.WriteLine($"serialize object count:{count}, newtonsoft used: {elapsedMilliseconds} seconds");
46 
47             elapsedMilliseconds = Serialize(count, () =>
48             {
49                 System.Text.Json.JsonSerializer.Serialize(new WeatherForecast
50                 {
51                     Date = DateTime.Now,
52                     Summary = "Hot",
53                     TemperatureCelsius = 88
54                 });
55             });
56             Console.WriteLine($"serialize object count:{count}, textjson used : {elapsedMilliseconds} seconds");
57             Console.ReadKey();
58 
59             /*
60              serialize object count:10000, newtonsoft used: 288 seconds
61             serialize object count:10000, textjson used : 45 seconds
62             ***************************************************
63             serialize object count:10000000, newtonsoft used: 10324 seconds
64             serialize object count:10000000, textjson used : 5681 seconds
65              */
66         }
67 
68         static long Serialize(int count, Action action)
69         {
70             Stopwatch stopwatch = Stopwatch.StartNew();
71             for (int i = count; i > 0; i--)
72             {
73                 action();
74             }
75             stopwatch.Stop();
76             var result = stopwatch.ElapsedMilliseconds;
77             stopwatch.Reset();
78             return result;
79         }
80     }
81     internal class WeatherForecast
82     {
83         public DateTimeOffset Date { get; set; }
84         public int TemperatureCelsius { get; set; }
85         public string Summary { get; set; }
86     }
87 }

 

當然如果加上JsonSerializerOptions,而且全部配置起來效能就會有所下降,畢竟這麼多配置在這呢,但是這樣也會更加靈活。

微軟文件裡面有各種介紹,不再詳述!

 從 Newtonsoft.Json 遷移到 System.Text.Json - .NET | Microsoft Docs

相關文章