C# – How to update appsettings.json programmatically

You have to overwrite the appsettings.json file to be able to update values programmatically. You have to deal with the whole file, not individual parts of it. The process can be summarized in the following steps: Load appsettings.json and deserialize it into an object. Update properties on the object. Serialize the object into a JSON … Read more

ASP.NET Core – How to unit test your middleware class

There are three requirements for unit testing a middleware class: Mock out RequestDelegate by defining a lambda that returns a Task (Task.FromException, Task.FromResult, or Task.FromCanceled). Mock out HttpContext by using DefaultHttpContext. The middleware function needs to be awaited, so your unit test needs to be defined with async Task. Here’s an example: This is a … Read more

ASP.NET Core – How to change the JSON serialization settings

System.Text.Json is the default JSON serializer in ASP.NET Core. It uses the following default serialization settings: To change the settings at the service level for all controllers, call AddJsonOptions() in Startup.ConfigureServices() like this: Note: This example is passing in JsonStringEnumConverter, which makes JsonSerializer use the enum name instead of enum value. When you change the … Read more

C# – Get and send JSON with HttpClient

The simplest way to get and send JSON with HttpClient is to use the GetFromJsonAsync() and PostAsJsonAsync() extension methods found in System.Net.Http.Json, like this: Note: You have to install the System.Net.Http.Json nuget package if you’re using a framework version before .NET 5. These extension methods use System.Text.Json for serialization. They simplify things by abstracting away … Read more

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 – Use JsonConverterFactory to serialize multiple types the same way

Let’s say you want to serialize the four datetime types – DateTime, DateTime?, DateTimeOffset, and DateTimeOffset? – in the same way. You want to serialize them to use the US date style (ex: 7/14/2021). There are two main ways to accomplish this: Create a custom converter for each type. Create a custom converter factory + … 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

System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime

When deserializing a datetime value with System.Text.Json.JsonSerializer, if the value isn’t in the format it expects, then it’ll throw a JsonException. It expects datetimes to be in the ISO-8601-1:2019 format (for example: 2021-07-12T12:35:34+00:00). For example, the following code is trying to deserialize a datetime value in an unexpected format: This’ll throw the following exception: System.Text.Json.JsonException: … Read more

System.Text.Json – How to customize serialization with JsonConverter

Most of the time JsonSerializer will get you want you want. You can pass in options to control serialization to a certain extent. But sometimes you’ll run into scenarios where you need to customize how it handles serialization for a specific type. This is where JsonConverter comes in. To customize serialization for a specific type, … Read more