Photo by Pankaj Patel Introduction When dealing with deserialization of JSON, it's always a good idea to validate that it infact deserialized correctly. But, how do you do that using System.Text.Json? We have the following json that we fetch from a third party, meaning we have no control of it. Usually, the JSON contains all the properties that we care about. But sometimes, some properties that we deem required are missing and we get a bunch of exceptions when trying to use the missing properties.
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by Josef Ottosson
C# - Tips and Tricks 02 - Named tuples
The problem We have the following code that returns a tuple. public class UserService { public (string, string) GetUser() { var firstName = "Josef"; var country = "Sweden"; return (firstName, country); } } We can then consume the data like this: [Fact] public void ShouldReturnFirstNameAndCountry() { var result = _sut.GetUser(); result.Item1.ShouldBe("Josef"); result.Item2.ShouldBe("Sweden"); } The problem with this approach is that we need to use Item1 and Item2 to access the property values.
C# - Tips and Tricks 01 - Covariant return types
The problem We have the following code: public abstract class MessageHandler { public abstract Message Poll(); } public abstract class Message { protected Message(string id, string correlationId, object data) { Id = id ?? throw new ArgumentNullException(nameof(id)); CorrelationId = correlationId ?? throw new ArgumentNullException(nameof(correlationId)); Data = data ??
Model bind multiple sources to a single class in ASP.NET Core
The problem We have the following action method: [Route("[controller]")] [ApiController] [Produces("application/json")] public class HomeController : ControllerBase { public IActionResult Post( [FromBody] PostRequestBody body, [FromHeader(Name = "Accept")] string accept, [FromHeader(Name = "X-Correlation-Id")] string correlationId, [FromQuery(Name = "filter")] string filter) { // Do something return Ok(); } } We want to consolidate all the parameters to one class, PostRequest, instead of...
Sorting large CSV files by multiple columns using C#
Photo by Alex Block This is a follow up to my previous post about sorting large files by column using c#.
ASP.NET Core 6 - HttpLogging - log requests/responses
Four years ago I wrote about how to log request/response headers in ASP.NET Core. This functionality has now been built into the framework, let's have a look. How I did it 4 years ago We shipped an application that was running behind a pretty...obscure WAF. They promised us that their product would not interfere in anyway with our application. That was wrong. They stripped almost all of our "custom" headers.
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with Josef
Contact Josef, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack