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

System.Text.Json.JsonException: A possible object cycle was detected which is not supported

When you use System.Text.Json.JsonSerializer to serialize an object that has a cycle, you’ll get the following exception: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 0. This is the same problem … Read more

JsonException: The JSON value could not be converted to Enum

When you’re using System.Text.Json to deserialize JSON that contains the string representation of an enum, you get the following exception: System.Text.Json.JsonException: The JSON value could not be converted to <Enum Type> The following JSON would cause this exception. Conference is an enum, and this is using the string representation “NFC” instead of the numeric value … Read more