
The above benchmark class is generic, so we can test all of the different objects with the same code, like this: BenchmarkRunner.Run>() Īfter running all class types with all the serializers, here are the results: Using var sr = new StreamReader(stream1) _dataContractJsonSerializer.WriteObject(stream1, _instance) Using (MemoryStream stream1 = new MemoryStream()) public string RunDataContractJsonSerializer() Return JsonConvert.SerializeObject( _instance) Return JsonSerializer.Serialize( _instance) _dataContractJsonSerializer = new DataContractJsonSerializer( typeof(T)) Private DataContractJsonSerializer _dataContractJsonSerializer Public class SerializeToString where T : new()
#JSON COMPARE NEWTONSOFT CODE#
The benchmark code itself is pretty straightforward (see on GitHub): The first thing we’ll test is serializing our different objects to a string. The Benchmark project itself is on GitHub.Įverything will be tested only in. For all benchmarks, I used BenchmarkDotNet with the following system: BenchmarkDotNet=v0.11.5, OS=Windows 4.1069 (1803/April2018Update/Redstone4) Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores.NET Core SDK=3.0.100. It’s not all of the required benchmarks, but it’s a pretty good indicator I think. A Dictionary with 1000 items (of the small class).A List with 1000 items (of the small class).A bigger class with about 25 properties, a DateTime and a couple of enums.A small class with just 3 primitive-type properties.Requests per second with an ASP.NET Core 3 applicationįor each, we’ll test different types of objects (which you can see in GitHub):.That’s a pretty big matrix of benchmarks, but I’ll try to do it as organized and concise as possible. And we’ll need to compare serialization targets: strings, streams, and char arrays (UTF-8 arrays). We’ll need to compare different types of classes (small and big), Lists, and Dictionaries. We’ll need to compare both serialization and deserialization.

Most notably, protobuf-net is a binary serializer that should be faster than any of the compared serializers in this article (though not verified in the benchmarks). Note that there are non-JSON serializers that are faster. Works with zero allocations and read/writes directly to the UTF8 binary for performance.

So why do we need a new serializer if we already got Newtonsoft.Json? While Newtonsoft.Json is great, there are several good reasons to replace it: NET for some time, then you should know the excellent Json.NET serializer, also known as Newtonsoft.Json. Their performance can significantly impact application performance as you’re about to see. These are major operations that happen on every request with objects. And when the server returns an object in its response, it serializes that object into JSON for your JavaScript client to understand. When your javascript client sends a JSON request in a POST body, the server uses JSON deserialization to convert it to a C# object. JSON serialization is a big factor in web applications.

This new serializer goes by the name and as the name suggests, all its classes are in that namespace. Besides C# 8 and support for WinForms & WPF, the new release added a brand new JSON (de)serializer. NET Core 3 was recently released and brought with it a bunch of innovations.
