C# – Convert an object to JSON

Converting an object to a JSON string is referred to as serialization. The best way to do serialization is by using a good JSON serializer. There are two primary options: I’ll show examples of both options below. Using JsonSerializer.Serialize() (in the built-in System.Text.Json) Here’s an example of using the built-in JsonSerializer.Serialize() (in System.Text.Json) to convert … Read more

C# – Serialize a tuple to JSON

When you serialize a tuple to JSON, it uses the the underlying ValueTuple’s field names – Item1 and Item2. It doesn’t matter if you’re using a named tuple, it won’t use the names you specified in the tuple declaration. This behavior is the same with Newtonsoft and System.Text.Json. Here’s an example of serializing a named … Read more