Table of Contents

JsonFormatterOptions Class

Definition

Specifies options that is related to JsonFormatter operations.

public class JsonFormatterOptions : IContentNegotiation, IExceptionDescriptorOptions, IValidatableParameterObject, IParameterObject
Inheritance
JsonFormatterOptions
Implements
Extension Methods

Examples

The following example demonstrates how to configure JsonFormatterOptions and use it with the JSON formatter to serialize objects.

using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Cuemon.Diagnostics;
using Cuemon.Extensions.Text.Json.Formatters;

namespace MyApp.Examples;

public class JsonFormatterOptionsExample
{
    public void SerializeWithCustomOptions()
    {
        // Create options with custom JSON serializer settings
        var options = new JsonFormatterOptions
        {
            Settings = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true,
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
                PropertyNameCaseInsensitive = true
            },
            SensitivityDetails = FaultSensitivityDetails.None
        };

        // Add custom converters
        options.Settings.Converters.Add(new JsonStringEnumConverter());

        var formatter = new JsonFormatter(options);
        var payload = new { UserName = "johndoe", Email = "john@example.com", Age = 30 };

        using (var stream = formatter.Serialize(payload))
        using (var reader = new StreamReader(stream))
        {
            string json = reader.ReadToEnd();
            Console.WriteLine(json);
            // Output:
            // {
            //   "userName": "johndoe",
            //   "email": "john@example.com",
            //   "age": 30
            // }
        }
    }

    public void ConfigureDefaultMediaType()
    {
        // Access the default media type for JSON
        Console.WriteLine(JsonFormatterOptions.DefaultMediaType); // "application/json"

        // DefaultConverters are applied automatically at static initialization
        Console.WriteLine($"Default converters configured: {JsonFormatterOptions.DefaultConverters != null}"); // true
    }

    public void ValidateOptions()
    {
        var options = new JsonFormatterOptions();
        // ValidateOptions throws InvalidOperationException if Settings is null
        options.ValidateOptions();
        Console.WriteLine("Options are valid.");
    }
}

Constructors

Name Description
JsonFormatterOptions()

Initializes a new instance of the JsonFormatterOptions class.

Properties

Name Description
DefaultConverters

Gets or sets a delegate that is invoked when JsonFormatterOptions is initialized and propagates registered JsonConverter implementations.

DefaultMediaType

Provides the default/fallback media type that the associated formatter should use when content negotiation either fails or is absent.

SensitivityDetails

Gets or sets a bitwise combination of the enumeration values that specify which sensitive details to include in the serialized result.

Settings

Gets or sets the settings to support the JsonFormatter.

SupportedMediaTypes

Gets or sets the collection of MediaTypeHeaderValue elements supported by the JsonFormatter.

Methods

Name Description
ValidateOptions()

Determines whether the public read-write properties of this instance are in a valid state.