Class JsonConverterCollectionExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Text.Json.Converters
- Assembly
- Cuemon.Extensions.AspNetCore.Text.Json.dll
Extension methods for the JsonConverter class.
public static class JsonConverterCollectionExtensions
- Inheritance
-
JsonConverterCollectionExtensions
Examples
The following example demonstrates how to register ASP.NET Core-specific JSON converters using the JsonConverterCollectionExtensions class.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Cuemon.Extensions.AspNetCore.Text.Json.Converters;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace MyApp.Examples;
public class JsonConverterCollectionExtensionsExample
{
public void Demonstrate()
{
var converters = new List<JsonConverter>();
// Add converter for IHeaderDictionary
converters.AddHeaderDictionaryConverter();
// Add converter for ProblemDetails
converters.AddProblemDetailsConverter();
// Add converter for HttpExceptionDescriptor
converters.AddHttpExceptionDescriptorConverter();
// Add converter for StringValues
converters.AddStringValuesConverter();
var options = new JsonSerializerOptions();
foreach (var converter in converters)
{
options.Converters.Add(converter);
// Example: serialize a HeaderDictionary
var headers = new HeaderDictionary
{
{ "X-Custom", "value1" },
{ "X-Another", "value2" }
};
string json = JsonSerializer.Serialize(headers, options);
Console.WriteLine(json);
// Output: {"X-Custom":"value1","X-Another":"value2"}
// Example: serialize StringValues
var values = new StringValues(new[] { "a", "b", "c" });
string svJson = JsonSerializer.Serialize(values, options);
Console.WriteLine(svJson);
// Output: ["a","b","c"]
}}
}
Methods
AddHeaderDictionaryConverter(ICollection<JsonConverter>)
Adds an IHeaderDictionary JSON converter to the collection.
public static ICollection<JsonConverter> AddHeaderDictionaryConverter(this ICollection<JsonConverter> converters)
Parameters
convertersICollection<JsonConverter>The collection of JsonConverter to extend.
Returns
- ICollection<JsonConverter>
A reference to
convertersso that additional calls can be chained.
AddHttpExceptionDescriptorConverter(ICollection<JsonConverter>, Action<ExceptionDescriptorOptions>)
Adds an HttpExceptionDescriptor JSON converter to the collection.
public static ICollection<JsonConverter> AddHttpExceptionDescriptorConverter(this ICollection<JsonConverter> converters, Action<ExceptionDescriptorOptions> setup = null)
Parameters
convertersICollection<JsonConverter>The collection of JsonConverter to extend.
setupAction<ExceptionDescriptorOptions>The ExceptionDescriptorOptions which may be configured.
Returns
- ICollection<JsonConverter>
A reference to
convertersso that additional calls can be chained.
AddProblemDetailsConverter(ICollection<JsonConverter>)
Adds a ProblemDetails JSON converter to the collection.
public static ICollection<JsonConverter> AddProblemDetailsConverter(this ICollection<JsonConverter> converters)
Parameters
convertersICollection<JsonConverter>The collection of JsonConverter to extend.
Returns
- ICollection<JsonConverter>
A reference to
convertersso that additional calls can be chained.
AddStringValuesConverter(ICollection<JsonConverter>)
Adds an StringValues JSON converter to the collection.
public static ICollection<JsonConverter> AddStringValuesConverter(this ICollection<JsonConverter> converters)
Parameters
convertersICollection<JsonConverter>The collection of JsonConverter to extend.
Returns
- ICollection<JsonConverter>
A reference to
convertersso that additional calls can be chained.