33

What is better to use when it comes to encoding and decoding of JSON in .NET? I have tried both and upto this point JsonConvert seems to be doing a good job. I have used JavaScriptSerializer in the past successfully but have had some problems in the recent past with it. Is it better to use JSON.NET than the .NET class?

What are the preferred functions for encoding/decoding json using the appropriate library? I use SerializeObject/DeSerializeObject from JSON.NET and Serialize/DeSerialize from .NET.

Thanks

Gabbar
  • 4,006
  • 7
  • 41
  • 78
  • 1
    There is [good comparison here](http://www.servicestack.net/mythz_blog/?p=344) in terms of speed. Even after reading this article I've used Newtonsoft.Json, as it did decent job for me. – toske Jun 29 '12 at 14:03
  • The Newtonsoft website provides a table of feature comparison and some benchmarks worth having a look at too: https://www.newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm – webStuff Feb 12 '19 at 08:30

2 Answers2

44

I think this is exactly the kind of comparison you are looking for.

It basically says that JSON.Net is better because it among other things...

  • Is faster
  • Has LINQ to JSON support
  • Can convert JSON to and from XML

In my opinion the only positive, (and it is a small positive), I can see for the built-in serializer is that there is no extra external dependency to manage.

Edit: Codeplex is shutting down one day so you can find the comparison here as well, just search for "Feature comparison" on the page.

Haohmaru
  • 901
  • 13
  • 19
  • _Is faster_ Did you test it? I tried with .NET 4.5.2 and Json.NET 9.01 and it's not at all faster than the JavaScriptSerializer. The speed stats in the official site are referring to JSON.NET 5...a little outdated! – krlzlx Sep 01 '16 at 15:26
  • 7
    @krlzlx .Net 4.5.2 was released in May 2015, and JSON.Net 9.01 a few months ago while I wrote this answer 3 years ago in 2013. Why don't you provide an updated answer with your metrics instead of being snarky! – Haohmaru Sep 01 '16 at 19:52
  • I'm sorry I know your answer is from 2013. But you didn't provide any metrics either except from the ones in the Json.NET website. I was disappointed when I tested it. That's all. – krlzlx Sep 01 '16 at 20:18
27

For a very long time, my app used JavascriptSerializer and saw no real reason to migrate. Even if performance comparisons claim huge percentage gains, we're talking milliseconds.

But here's one very very good reason to migrate: JavascriptSerializer isn't available in .Net Core because it's part of System.Web So if you're using JavascriptSerializer, you're stuck and have to migrate to JSON.net

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    asp net core uses json.net by default. – Liang Apr 04 '18 at 08:05
  • 1
    .Net core now has the [System.Text.Json serialisers](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to) so this is no longer correct. [MS now want you to use this serialiser not the Newtonsoft one](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to). Though to echo your point (above) I haven't seen a need to move TBH – Liam Sep 15 '20 at 09:02