In .net core 3.0
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json;
using Google.Protobuf;
using System.Diagnostics;
using ConsoleApp389.Models;
namespace ConsoleApp389
{
class Program
{
static void Main(string[] args)
{
TestCostDemo();
Console.ReadLine();
}
static void TestCostDemo()
{
List<SalesOrderDetail> dataList = GetList();
StringBuilder msgBuilder = new StringBuilder();
Stopwatch sw = new Stopwatch();
//sw.Start();
//Console.WriteLine($"BinaryFormatterDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
//BinaryFormatterDemo<SalesOrderDetail>(dataList);
//sw.Stop();
//Console.WriteLine($"BinaryFormatterDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
//msgBuilder.AppendLine($"BinaryFormatterDemo cost {sw.ElapsedMilliseconds} milliseconds.");
sw.Restart();
Console.WriteLine($"NewtonJsonDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
NewtonJsonDemo<SalesOrderDetail>(dataList);
sw.Stop();
Console.WriteLine($"NewtonJsonDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
msgBuilder.AppendLine($"NewtonJsonDemo cost {sw.ElapsedMilliseconds} milliseconds.");
sw.Restart();
Console.WriteLine($"JsonSerializerDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
JsonSerializerDemo<SalesOrderDetail>(dataList);
sw.Stop();
Console.WriteLine($"JsonSerializerDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
msgBuilder.AppendLine($"JsonSerializerDemo cost {sw.ElapsedMilliseconds} milliseconds.");
string msg = msgBuilder.ToString();
Console.WriteLine(msg);
System.Diagnostics.Debug.WriteLine(msg);
}
static List<SalesOrderDetail> GetList()
{
using(AdventureWorks2017Context db=new AdventureWorks2017Context())
{
List<SalesOrderDetail> dataList = new List<SalesOrderDetail>(db.SalesOrderDetail);
return dataList;
}
}
static void BinaryFormatterDemo<T>(List<T> dataList)
{
using (FileStream fs = new FileStream($"{Guid.NewGuid().ToString().Substring(0,6)}.bin", FileMode.Create))
{
BinaryFormatter binFormatter = new BinaryFormatter();
try
{
binFormatter.Serialize(fs, dataList);
}
catch(SerializationException e)
{
Console.WriteLine($"BinaryFormatter serialize ex:{e.StackTrace}");
}
}
}
static void NewtonJsonDemo<T>(List<T> dataList)
{
try
{
string jsonString = JsonConvert.SerializeObject(dataList);
}
catch(SerializationException ex)
{
Console.WriteLine($"NewtonJson serialization ex :{ex.StackTrace}");
}
}
static void JsonSerializerDemo<T>(List<T> dataList)
{
try
{
string msJsonSerializerString = System.Text.Json.JsonSerializer.Serialize(dataList);
}
catch(SerializationException ex)
{
Console.WriteLine($"System.Text.Json.JsonSerializer ex:{ex.StackTrace}");
}
}
//static void GoogleProtoBufDemo<T>(List<T> dataList)
//{
// using (var file = File.Create("person.bin"))
// {
// ProtoBuf.Serializer.Serialize<T>(file, dataList);
// }
//}
}
}
In .net 4.8
using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace ConsoleApp390 { class Program { static void Main(string[] args) { TestCostDemo(); Console.ReadLine(); } static void TestCostDemo() { List<SalesOrderDetail> dataList = GetList(); StringBuilder msgBuilder = new StringBuilder(); Stopwatch sw = new Stopwatch(); //sw.Start(); //Console.WriteLine($"BinaryFormatterDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); //BinaryFormatterDemo<SalesOrderDetail>(dataList); //sw.Stop(); //Console.WriteLine($"BinaryFormatterDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); //msgBuilder.AppendLine($"BinaryFormatterDemo cost {sw.ElapsedMilliseconds} milliseconds."); sw.Restart(); Console.WriteLine($"NewtonJsonDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); NewtonJsonDemo<SalesOrderDetail>(dataList); sw.Stop(); Console.WriteLine($"NewtonJsonDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); msgBuilder.AppendLine($"NewtonJsonDemo cost {sw.ElapsedMilliseconds} milliseconds."); sw.Restart(); Console.WriteLine($"JsonSerializerDemo started at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); JsonSerializerDemo<SalesOrderDetail>(dataList); sw.Stop(); Console.WriteLine($"JsonSerializerDemo stopped at {DateTime.Now.ToString("yyyyMMddHHmmssffff")}"); msgBuilder.AppendLine($"JsonSerializerDemo cost {sw.ElapsedMilliseconds} milliseconds."); string msg = msgBuilder.ToString(); Console.WriteLine(msg); System.Diagnostics.Debug.WriteLine(msg); } static List<SalesOrderDetail> GetList() { using (AdventureWorks2017Entities db = new AdventureWorks2017Entities()) { List<SalesOrderDetail> dataList = new List<SalesOrderDetail>(db.SalesOrderDetails); return dataList; } } static void BinaryFormatterDemo<T>(List<T> dataList) { using (FileStream fs = new FileStream($"{Guid.NewGuid().ToString().Substring(0, 6)}BinFormatter.dat", FileMode.Create)) { BinaryFormatter binFormatter = new BinaryFormatter(); try { binFormatter.Serialize(fs, dataList); } catch (SerializationException e) { Console.WriteLine($"BinaryFormatter serialize ex:{e.StackTrace}"); } } } static void NewtonJsonDemo<T>(List<T> dataList) { try { string jsonString = JsonConvert.SerializeObject(dataList); } catch (SerializationException ex) { Console.WriteLine($"NewtonJson serialization ex :{ex.StackTrace}"); } } static void JsonSerializerDemo<T>(List<T> dataList) { try { string msJsonSerializerString = System.Text.Json.JsonSerializer.Serialize(dataList); } catch (SerializationException ex) { Console.WriteLine($"System.Text.Json.JsonSerializer ex:{ex.StackTrace}"); } } } }
I‘ll list serveral executed results.
In .net NewtonJson is faster than System.Text.Json.JsonSerializer.Serialize.The validated result as below.
1.
NewtonJsonDemo cost 1290 milliseconds.
JsonSerializerDemo cost 1404 milliseconds.
2.
NewtonJsonDemo cost 1213 milliseconds.
JsonSerializerDemo cost 1814 milliseconds.
3.
NewtonJsonDemo cost 1305 milliseconds.
JsonSerializerDemo cost 1373 milliseconds.
4.
NewtonJsonDemo cost 1329 milliseconds.
JsonSerializerDemo cost 1749 milliseconds.
5.
NewtonJsonDemo cost 1234 milliseconds.
JsonSerializerDemo cost 1395 milliseconds.
While in .net core 3.0 System.Text.Json.JsonSerializer.Serialize is faster than NewtonJson.
1.
NewtonJsonDemo cost 1704 milliseconds.
JsonSerializerDemo cost 1247 milliseconds.
2.
NewtonJsonDemo cost 1596 milliseconds.
JsonSerializerDemo cost 1451 milliseconds.
3.
NewtonJsonDemo cost 1430 milliseconds.
JsonSerializerDemo cost 1221 milliseconds.
4.
NewtonJsonDemo cost 1390 milliseconds.
JsonSerializerDemo cost 1219 milliseconds.
5.
NewtonJsonDemo cost 1334 milliseconds.
JsonSerializerDemo cost 1216 milliseconds.
Totally speaking, the .net core is completely faster than .net both in NewtonJson and System.Text.Json.JsonSerializer.Serialize
The data source come from Adventureworks2017.Sales.SalesOrderDetail table which has 121317 rows .