C# object to JSON in .NET: Secrets of Speed and Performance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Every C# Developer Should Know How to Serialize Objects to JSON

\n\n

Have you ever wondered how many times a day your code turns complex C# objects into compact JSON strings? Working with web APIs, saving configurations, transferring data between microservices — everywhere we encounter serialization. The question "How do I turn a C# object into a JSON string in .NET?" is one of the most popular on Stack Overflow, and for good reason. How exactly you do it determines the speed of your application and its responsiveness.

\n\n

In this article, we will not just show how to convert an object to a JSON string. We will analyze optimization and performance of this process: from choosing a library to fine-tuning the serializer. You will learn how to avoid common mistakes that slow down your application and learn to write code that works lightning fast.

\n\n

Basic Method: How to Turn a C# Object into a JSON String

\n\n

Let's start with the classics. Suppose we have a simple class User:

\n\n
public class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public string Email { get; set; }\n}
\n\n

The simplest way to serialize it is to use Newtonsoft.Json (also known as Json.NET):

\n\n
using Newtonsoft.Json;\n\nvar user = new User { Id = 1, Name = "Alexey", Email = "alex@example.com" };\nstring json = JsonConvert.SerializeObject(user);\nConsole.WriteLine(json); \n// {"Id":1,"Name":"Alexey","Email":"alex@example.com"}
\n\n

With the release of .NET Core 3.0, the native System.Text.Json appeared — a fast and lightweight alternative:

\n\n
using System.Text.Json;\n\nvar user = new User { Id = 1, Name = "Alexey", Email = "alex@example.com" };\nstring json = JsonSerializer.Serialize(user);\nConsole.WriteLine(json);\n// {"Id":1,"Name":"Alexey","Email":"alex@example.com"}
\n\n

It seems there is no difference. But when your application processes thousands of requests per second, the choice of serializer becomes critical.

\n\n

Performance: Newtonsoft.Json vs System.Text.Json

\n\n

Let's look at serialization performance in .NET 8. In tests with 1 million operations:

\n\n
    \n
  • System.Text.Json — on average 40-60% faster than Newtonsoft.Json when serializing simple objects.
  • \n
  • Newtonsoft.Json — loses in speed but wins in flexibility: supports more custom converters and attributes.
  • \n
  • Memory consumption — System.Text.Json generates less garbage, reducing the load on the garbage collector (GC).
  • \n
\n\n

However, there is a nuance: if you need support for JsonIgnore, JsonPropertyName, or complex converters, Newtonsoft.Json is still indispensable. But for most modern projects on .NET 6+, System.Text.Json is recommended.

\n\n

Serialization Optimization: 5 Practical Tips

\n\n

Now let's move on to the most interesting part — how to make your code even faster.

\n\n

1. Use source generators

\n\n

System.Text.Json supports source generation — this compiles serialization code at build time, not at runtime. Result: 2-3 times faster!

\n\n
[JsonSerializable(typeof(User))]\npublic partial class UserContext : JsonSerializerContext\n{\n}\n\n// Usage:\nstring json = JsonSerializer.Serialize(user, UserContext.Default.User);
\n\n

This approach is especially effective for large models and high-load applications.

\n\n

2. Configure the serializer for your tasks

\n\n

Don't use default settings. Disable unnecessary options:

\n\n
var options = new JsonSerializerOptions\n{\n    PropertyNamingPolicy = JsonNami                

Blogs

Book Recommendations