0

The Json.NET NuGet package provides two classes to work with Json objects. These are JsonSerializer and JsonConvert. I need to use them to serialize an Json string(an api response). The json string is huge.

I know that JsonConvert is more like a wrapper on top of the JsonSerializer and a couple of people suggested me to avoid using them.

Which one of them needs to be used for better performance and lower overhead?

  • What kind of application is this? If it's an ASP.NET project then just let the framework handle how the object get serialized. – ProgrammingLlama Jun 20 '21 at 15:03
  • @Llama, I'm working with a console app that deals with json strings. Even if its a ASP.NET project, I still need to used one of the two if I don't want the default .NET Json serializers right? – Vamsi Krishna Jun 20 '21 at 15:06

1 Answers1

3

JsonSerializer is an instantiable class that you can use to deserialize JSON to objects or serialize objects to JSON. JsonSerializer works with JsonReader and JsonWriter classes to read and write JSON. So there's a little bit more code involved in using it. However, it gives you the most flexibility, including being able to use streams, which is important for working with huge JSON files. See How to parse huge JSON file as stream in Json.NET? for more information about that.

JsonConvert, on the other hand, is a static class. It provides convenience methods that make it easy to deserialize or serialize using minimal code. Under the covers, this class uses JsonSerializer, as you surmised. But, crucially, JsonConvert does not provide overloads for working with streams. Instead, all of its methods work with strings. So its intended use case is when the JSON to deserialize (or object model to serialize) is small and already in memory.

Since you said your JSON is huge, you will want to use JsonSerializer.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300