C# – How to read problem details JSON with HttpClient

Problem details (RFC7807) is a standardized error response format that has a Content-Type of application/problem+json, an error response code (i.e. 400 – Bad Request), and has a response body that looks like this: This can be extended to include any number of properties. The example shown above comes from the default way ASP.NET Core returns … Read more

C# – JSON object contains a trailing comma at the end which is not supported

Problem When you deserialize JSON, you get the following error: The JSON object contains a trailing comma at the end which is not supported in this mode. Change the reader options. JSON properties are separated with commas. A trailing comma is one that has no properties after it. Here’s an example of a trailing comma: … Read more

C# – Deserialize JSON with a specific constructor

When your class has multiple constructors, you can use the JsonConstructor attribute to specify which constructor to use during deserialization. Here’s an example: Note: JsonConstructor for System.Text.Json was added in .NET 5. Now deserialize: This outputs: This shows that it used the Person(int luckyNumber) constructor. It passed in the LuckyNumber JSON property to the constructor, … Read more

C# – Deserialize JSON to a derived type

The simplest way to deserialize JSON to a derived type is to put the type name in the JSON string. Then during deserialization, match the type name property against a set of known derived types and deserialize to the target type. System.Text.Json doesn’t have this functionality out of the box. That’s because there’s a known … Read more

C# – Deserialize a JSON array to a list

When you’re working with a JSON array, you can deserialize it to a list like this: This deserializes all of the objects in the JSON array into a List<Movie>. You can use this list object like usual. Note: All examples will use System.Collections.Generic and System.Text.Json. I’ll exclude the using statements for brevity. Example – JSON … Read more

C# – Deserialize JSON as a stream

Here’s an example of deserializing a JSON file as a stream with System.Text.Json: Stream deserialization has three main benefits: It’s memory-efficient, which improves overall performance. Fail fast when there’s a problem in the JSON data. Deserialization process can be canceled (async version only). In this article, I’ll go into details about these benefits and show … Read more

C# – Examples of using GroupBy()

Here’s an example of using GroupBy() to group coders by language: This example outputs the following: GroupBy() produces groups that contain the grouping key (i.e. Language) and the list of objects in the group (i.e. the Coder objects). The GroupBy() syntax is complex because it supports many scenarios. You can select one or more grouping … Read more

System.Text.Json – Deserialize properties that aren’t part of the class

Use the JsonExtensionData attribute to simplify accepting additional properties in JSON that aren’t part of the class you’re deserializing to. To use this attribute, add a compatible* property to the class and apply the JsonExtensionData attribute: *Compatible property types you can use are Dictionary<string, object>, Dictionary<string, JsonElement> and JsonObject. Any property in the JSON that’s … Read more

System.Text.Json – Control the order that properties get serialized

You can use the JsonPropertyOrder attribute to control the order that properties get serialized. You specify the order as an integer, and it serializes the properties in ascending order. Here’s an example: Note: Properties have a default order value of 0. Now serialize a Programmer object: This generates the following JSON with the properties serialized … Read more

System.Text.Json – Apply a custom converter to a specific property

When you create a custom converter, you create it to handle a specific type (such as DateTime). When you add this converter to JsonSerializerOptions.Converters, it applies the converter to all properties of that type (i.e. all DateTime properties). How about if you want to apply a custom converter to a specific property? To do that, … Read more