System.Text.Json – Using anonymous types to customize serialization

It’s common to need to customize serialization. When you need to do this, you’d typically need to create a custom converter and pass it in during serialization. Depending on your scenario, an alternative approach is to use anonymous types, like this: Basically you select properties from another object and format them as desired, and then … Read more

System.Text.Json – How to serialize non-public properties

By default, the System.Text.Json.JsonSerializer only serializes public properties. If you want to serialize non-public properties, you can create a custom converter and use reflection to get the non-public properties too, like this: Use the custom converter by adding it to JsonSerializerOptions.Converters and passing the options in when serializing, like this: In this article, I’ll show … Read more

C# – Access modifiers

When you’re using a Queue class, you only need access to the Enqueue() and Dequeue() methods. Everything else is private. You don’t need to know how data is stored or managed internally. That’s encapsulation, and it’s enabled by access modifiers. Access modifiers are used to hide members from other code. In C#, there are four … Read more