Table of Contents

Class MinimalJsonOptions

Namespace
Cuemon.Extensions.AspNetCore.Text.Json
Assembly
Cuemon.Extensions.AspNetCore.Text.Json.dll

A ConfigureOptions<TOptions> implementation which will pass JsonFormatterOptions to JsonOptions.

public class MinimalJsonOptions : ConfigureOptions<JsonOptions>, IConfigureOptions<JsonOptions>
Inheritance
MinimalJsonOptions
Implements
Inherited Members
Extension Methods

Examples

The following example demonstrates how to register in an ASP.NET Core application to propagate custom into the minimal API JSON serialization pipeline.

using System;
using System.Text.Json;
using Cuemon.Extensions.AspNetCore.Text.Json;
using Cuemon.Extensions.Text.Json.Formatters;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace MyApp.Examples;

public class MinimalJsonOptionsExample
{
    public void Demonstrate()
    {
        var services = new ServiceCollection();

        // Configure JsonFormatterOptions with custom settings
        services.Configure<JsonFormatterOptions>(o =>
        {
            o.Settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            o.Settings.WriteIndented = false;
        });

        // Register MinimalJsonOptions so that JsonOptions (minimal API)
        // receives the same settings and converters
        services.AddTransient<IConfigureOptions<JsonOptions>, MinimalJsonOptions>();

        var provider = services.BuildServiceProvider();
        var jsonOptions = provider.GetRequiredService<IOptions<JsonOptions>>();

        Console.WriteLine($"Property naming policy: {jsonOptions.Value.SerializerOptions.PropertyNamingPolicy}"); // CamelCase
        Console.WriteLine($"Write indented: {jsonOptions.Value.SerializerOptions.WriteIndented}");               // False

}
}

Constructors

MinimalJsonOptions(IOptions<JsonFormatterOptions>)

Initializes a new instance of the MinimalJsonOptions class.

public MinimalJsonOptions(IOptions<JsonFormatterOptions> formatterOptions)

Parameters

formatterOptions IOptions<JsonFormatterOptions>

The formatter options.