# Newtonsoft.Json .NET - To be, or not to be?⁸

**URL:** <https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160>\
**Category:** dev\
**Created:** [February 11, 2020, 3:14pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160 "2020-02-11T15:14:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatarincev](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/tatarincev/32/161_2.png) [@tatarincev](https://www.virtocommerce.org/u/tatarincev)\
**Post date:** [February 11, 2020, 3:14pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/1 "2020-02-11T15:14:31Z")

</div>

JSON serialization is a big factor in web applications, especially for applications that expose almost all their functionality over REST API as our Virto platform does, and the performance of object serialization or deserialization has a significant impact on overall application performance and responsiveness.

Starting with 2.x Virto platform versions and till to the newest [3.0-rc3](https://github.com/VirtoCommerce/vc-platform/releases/tag/3.0.0-rc.3) we have been using with **Newtonsoft.Json .NET** serializer for Web API and basically were happy with it until the last moment, on one of our projects we faced with big API response latency for big size response with 1Mb body size. The average response time was more than **10 seconds!!!** and was completely unacceptable.

Digging into this problem and conduct a series of tests we end up on one suspect, it was a JSON serializer - we use **Newtonsoft.Json .NET 12.0.3** that integrated into both [ASP.NET](http://ASP.NET) versions.

Durinng investigations we found this excellent article [The Battle of C# to JSON Serializers in .NET Core 3](https://michaelscodingspot.com/the-battle-of-c-to-json-serializers-in-net-core-3/) that contains the benchmarks of six the most popular JSON serializer. But any of time charts from this article doesn’t show the big difference between **Newtonsoft.Json** and embedded into [ASP.NET](http://ASP.NET) Core **System.Text.Json** serializers.

 ![image](https://canada1.discourse-cdn.com/flex032/uploads/virtocommerce/original/1X/21a915fb98ffdea201c5055aaf1cdb9929e80acb.png)

And to confirm our doubts we conducted our own benchmark. We created an empty _pure_ [ASP.NET](http://ASP.NET) Core Web API project from the templates proposed by Visual Studio 2019. Defined the one API endpoint that returned three responses with different sizes ( **1.6Mb, 16Mb, and 160Mb** ) respectively and measured response times for using **Newtonsoft.Json** and embedded into [ASP.NET](http://ASP.NET) Core 3.0 **System.Text.Json** serializers.

Here is our benchmark

 ![image](https://canada1.discourse-cdn.com/flex032/uploads/virtocommerce/original/1X/2a405672766b81172ad28135ef535b37cb0ade3c.png)

The benchmark shows that for serialization with using **System.Text.Json** is **10** times faster than **Newtonsoft.Json** library.

The answer to the question why the results of our tests for single requests are different from those described in this article, we found later in the same article.

> The only surprise here is how poorly **Newtonsoft.Json** performed. This is probably due to the UTF-16 and UTF-8 issue. The HTTP protocol works with UTF-8 text. Newtonsoft converts this text into .NET `string` types, which are UTF-16. This overhead is not done in either **Utf8Json** or **System.Text.Json** , which work directly with UTF-8.

## Conclusions

**Does this mean we should all change to System.Text.Json or Utf8Json?**  
Answer - **yes**! The **System.Text.Json** is looking as better replacement due to this library is well supported and Microsoft will continue to invest resources and effort into **System.Text.Json** so we are going to get excellent support.  
But before replacement, we need to ensure that migration to the new serializer won’t affect exists functionality and doesn’t introduce any breaking changes for existing logic.

---

<div class="post-metadata">

**Author:** ![OlegoO](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/olegoo/32/27_2.png) [@OlegoO](https://www.virtocommerce.org/u/OlegoO)\
**Post date:** [February 11, 2020, 3:22pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/2 "2020-02-11T15:22:44Z")

</div>



---

<div class="post-metadata">

**Author:** ![OlegoO](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/olegoo/32/27_2.png) [@OlegoO](https://www.virtocommerce.org/u/OlegoO)\
**Post date:** [February 11, 2020, 7:20pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/3 "2020-02-11T19:20:43Z")

</div>

I found more details here [https://michaelscodingspot.com/the-battle-of-c-to-json-serializers-in-net-core-3/](https://michaelscodingspot.com/the-battle-of-c-to-json-serializers-in-net-core-3/)

The only surprise here is how poorly **Newtonsoft.Json** performed. This is probably due to the UTF-16 and UTF-8 issue. The HTTP protocol works with UTF-8 text. Newtonsoft converts this text into .NET `string` types, which are UTF-16. This overhead is not done in either **Utf8Json** or **System.Text.Json** , which work directly with UTF-8.

---

<div class="post-metadata">

**Author:** ![asvishnyakov](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/asvishnyakov/32/2722_2.png) [@asvishnyakov](https://www.virtocommerce.org/u/asvishnyakov)\
**Post date:** [February 11, 2020, 8:12pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/4 "2020-02-11T20:12:12Z")

</div>

Please, add source code of benchmark

---

<div class="post-metadata">

**Author:** ![pushnitsa](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/pushnitsa/32/250_2.png) [@pushnitsa](https://www.virtocommerce.org/u/pushnitsa)\
**Post date:** [February 17, 2020, 8:57am UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/5 "2020-02-17T08:57:18Z")

</div>

Do we have a possibility to replace nestonsoft.json by system.text.json in the autorest clients?

And what about non [ASP.NET](http://ASP.NET) Core projects? Is they have some replacement newtonsoft.json?

---

<div class="post-metadata">

**Author:** ![OlegoO](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/olegoo/32/27_2.png) [@OlegoO](https://www.virtocommerce.org/u/OlegoO)\
**Post date:** [February 17, 2020, 1:15pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/6 "2020-02-17T13:15:46Z")

</div>

> [@pushnitsa](#):
>
> Do we have a possibility to replace nestonsoft.json by system.text.json in the autorest clients?

This work is already done in the V3 generator.

> **[GitHub - Azure/autorest.csharp: Extension for AutoRest...](https://github.com/Azure/autorest.csharp)**
>
> Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code - GitHub - Azure/autorest.csharp: Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code

> <https://github.com/Azure/autorest.csharp/issues/249>
>
> Hi,
> I didn't find a similar proposal in the issues so here it goes. ASP.NET Cor…e 3.0 uses by default the new System.Text.Json serializer/deserializer which is supposed to be as much allocation free as possible from what I heard. I think it might be a nice addition if while generating autorest client developer could select between Newtonsoft and this new System.Json.Text serializer/deserializer. Out of curiosity, I created a proof of concept locally to compare the performance between these two libraries. PoC considered an endpoint that returns 200 with 20 items (2,44KB). The tests were performed "InMemory" (with delegating handler returning a response immediately) and "RealWorld" (with simple localhost ASP.NET Core 3.0 application returning result).
> 
> The PoC is right now tangled a little with my other local projects so I won't upload it right now but If the proposal has a chance of being implemented I can create a more sophisticated PoC to explore this topic further.
> 
> I haven't analyzed it deeply enough to know which of the current Newtonsoft serializer/deserializer options would not be supported when using System.Text.Json ones but I think if choosing a serializer/deserializer would be an option then it could be chosen by some of the developers (given that ASP.NET Core 3.0 uses it by default). And maybe become the default in the future.
> 
> The results are as follows (seeings 12% time reduction and 40% allocation/gc reduction in real world scenario) :
> !\[image\](https://user-images.githubusercontent.com/20770017/69008173-db9c4100-0947-11ea-811d-a3e3ee2111f8.png)

---

<div class="post-metadata">

**Author:** ![pushnitsa](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/pushnitsa/32/250_2.png) [@pushnitsa](https://www.virtocommerce.org/u/pushnitsa)\
**Post date:** [February 17, 2020, 1:31pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/7 "2020-02-17T13:31:12Z")

</div>

Thank you. But it’s not useful for us, till we migrate to .NET Core version, right?

---

<div class="post-metadata">

**Author:** ![OlegoO](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/olegoo/32/27_2.png) [@OlegoO](https://www.virtocommerce.org/u/OlegoO)\
**Post date:** [February 18, 2020, 6:43am UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/8 "2020-02-18T06:43:30Z")

</div>

@pushnitsa you can install System.Text.Json NuGet package [https://www.nuget.org/packages/System.Text.Json](https://www.nuget.org/packages/System.Text.Json) for .NET Framework 4.6 and later versions.

---

<div class="post-metadata">

**Author:** ![pushnitsa](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/pushnitsa/32/250_2.png) [@pushnitsa](https://www.virtocommerce.org/u/pushnitsa)\
**Post date:** [February 18, 2020, 8:49am UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/9 "2020-02-18T08:49:28Z")

</div>

Seems to be it is still require .net core ☹

 ![image](https://canada1.discourse-cdn.com/flex032/uploads/virtocommerce/original/1X/bd87d01d1ea4a046e265cd10481937287a273390.png)

---

<div class="post-metadata">

**Author:** ![tatarincev](https://yyz2.discourse-cdn.com/flex032/user_avatar/www.virtocommerce.org/tatarincev/32/161_2.png) [@tatarincev](https://www.virtocommerce.org/u/tatarincev)\
**Post date:** [November 30, 2020, 12:59pm UTC](https://www.virtocommerce.org/t/newtonsoft-json-net-to-be-or-not-to-be/160/10 "2020-11-30T12:59:49Z")

</div>

Seems it is a well-known issue [High disk IO volume after upgrading to 3.0 · Issue #17843 · dotnet/aspnetcore · GitHub](https://github.com/dotnet/aspnetcore/issues/17843)

> The Newtonsoft.Json based output-formatter buffers content. The buffering is done to avoid synchronous writes to the HTTP response since the serializer is entirely synchronous.

> At any given time, the buffer keeps 32k in memory, followed by writing further content to disk. Consequently, your disk I/affects the response time for payloads larger than 32k. We’ve had a similar discussion about this: #17843. #17843 (comment) has some guidance on what you could consider doing in your application.
