Table of Contents

JsonConverterCollectionExtensions Class

Definition

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

Name Description
AddHeaderDictionaryConverter(ICollection<JsonConverter>)

Adds an IHeaderDictionary JSON converter to the collection.

AddHttpExceptionDescriptorConverter(ICollection<JsonConverter>, Action<ExceptionDescriptorOptions>)

Adds an HttpExceptionDescriptor JSON converter to the collection.

AddProblemDetailsConverter(ICollection<JsonConverter>)

Adds a ProblemDetails JSON converter to the collection.

AddStringValuesConverter(ICollection<JsonConverter>)

Adds an StringValues JSON converter to the collection.