Introduction: Why Every C# Developer Should Know How to Serialize Objects to JSON
\n\nHave 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\nIn 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\nBasic Method: How to Turn a C# Object into a JSON String
\n\nLet's start with the classics. Suppose we have a simple class User:
public class User\n{\n public int Id { get; set; }\n public string Name { get; set; }\n public string Email { get; set; }\n}\n\nThe simplest way to serialize it is to use Newtonsoft.Json (also known as Json.NET):
\n\nusing 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\nWith the release of .NET Core 3.0, the native System.Text.Json appeared — a fast and lightweight alternative:
\n\nusing 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\nIt seems there is no difference. But when your application processes thousands of requests per second, the choice of serializer becomes critical.
\n\nPerformance: Newtonsoft.Json vs System.Text.Json
\n\nLet'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
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.
Serialization Optimization: 5 Practical Tips
\n\nNow let's move on to the most interesting part — how to make your code even faster.
\n\n1. Use source generators
\n\nSystem.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\nThis approach is especially effective for large models and high-load applications.
\n\n2. Configure the serializer for your tasks
\n\nDon't use default settings. Disable unnecessary options:
\n\nvar options = new JsonSerializerOptions\n{\n PropertyNamingPolicy = JsonNami