JsonConverterCollectionExtensions Class
Definition
- Namespace
- Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters
- Assemblies
- Codebelt.Extensions.AspNetCore.Newtonsoft.Json.dll
Extension methods for the Newtonsoft.Json.JsonConverter class.
public static class JsonConverterCollectionExtensions
- Inheritance
-
JsonConverterCollectionExtensions
Examples
ASP.NET Core applications frequently need to serialize framework types like HttpExceptionDescriptor, ProblemDetails, and StringValues in error responses, diagnostic logs, and middleware context. Out-of-the-box JSON.NET doesn't know how to handle these types efficiently, producing verbose or incorrect output that doesn't match REST API conventions. The JsonConverterCollectionExtensions class provides specialized converter registration methods for these ASP.NET Core-specific types, enabling clean, RFC-compliant serialization without custom marshaling. These extension methods enable seamless JSON serialization of ASP.NET Core framework types and improve error response handling with standardized problem details format. This example demonstrates adding converters for HTTP exception descriptors and observing the registered behavior:
using System;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.Diagnostics;
using Newtonsoft.Json;
namespace Examples;
class HttpExceptionDescriptorConverterExample
{
static void Main()
{
var settings = new JsonSerializerSettings();
// Add HTTP exception descriptor converter with custom sensitivity settings
settings.Converters.AddHttpExceptionDescriptorConverter(setup =>
{
setup.SensitivityDetails = FaultSensitivityDetails.StackTrace | FaultSensitivityDetails.Data;
});
Console.WriteLine("HTTP exception converter registered");
}
}
Adding a Problem Details Converter
The following example demonstrates adding a converter for ProblemDetails responses:
using System;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Examples;
class ProblemDetailsConverterExample
{
static void Main()
{
var settings = new JsonSerializerSettings();
// Add problem details converter for RFC 7807 responses
settings.Converters.AddProblemDetailsConverter();
var problemDetails = new ProblemDetails
{
Type = "https://example.com/errors/validation-failed",
Title = "One or more validation errors occurred.",
Status = 422,
Detail = "The request body contains invalid data."
};
var json = JsonConvert.SerializeObject(problemDetails, settings);
Console.WriteLine(json);
}
}
Adding a StringValues Converter
The following example demonstrates adding a converter for StringValues (HTTP header values):
using System;
using Codebelt.Extensions.AspNetCore.Newtonsoft.Json.Converters;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
namespace Examples;
class StringValuesConverterExample
{
static void Main()
{
var settings = new JsonSerializerSettings();
// Add StringValues converter for serializing HTTP header collections
settings.Converters.AddStringValuesConverter();
var headerValues = new StringValues(new[] { "application/json", "text/plain" });
var json = JsonConvert.SerializeObject(headerValues, settings);
Console.WriteLine(json);
}
}
These extension methods enable seamless JSON serialization of ASP.NET Core framework types and improve error response handling with standardized problem details format.
Methods
| Name | Description |
|---|---|
| AddHttpExceptionDescriptorConverter(ICollection<JsonConverter>, Action<ExceptionDescriptorOptions>) | Adds an HttpExceptionDescriptor JSON converter to the list. |
| AddProblemDetailsConverter(ICollection<JsonConverter>) | Adds a ProblemDetails JSON converter to the list. |
| AddStringValuesConverter(ICollection<JsonConverter>) | Adds an StringValues JSON converter to the list. |