Class JsonFormatterOptions
- Namespace
- Cuemon.Extensions.Text.Json.Formatters
- Assembly
- Cuemon.Extensions.Text.Json.dll
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
JsonFormatterOptions()
Initializes a new instance of the JsonFormatterOptions class.
public JsonFormatterOptions()
Remarks
The following table shows the initial property values for an instance of JsonFormatterOptions.
| Property | Initial Value |
|---|---|
| Settings | JsonSerializerOptions |
| SensitivityDetails | None |
| SupportedMediaTypes |
|
Properties
DefaultConverters
Gets or sets a delegate that is invoked when JsonFormatterOptions is initialized and propagates registered JsonConverter implementations.
public static Action<IList<JsonConverter>> DefaultConverters { get; set; }
Property Value
- Action<IList<JsonConverter>>
The delegate which propagates registered JsonConverter implementations when JsonFormatterOptions is initialized.
DefaultMediaType
Provides the default/fallback media type that the associated formatter should use when content negotiation either fails or is absent.
public static MediaTypeHeaderValue DefaultMediaType { get; }
Property Value
- MediaTypeHeaderValue
The 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.
public FaultSensitivityDetails SensitivityDetails { get; set; }
Property Value
- FaultSensitivityDetails
The enumeration values that specify which sensitive details to include in the serialized result.
Settings
Gets or sets the settings to support the JsonFormatter.
public JsonSerializerOptions Settings { get; set; }
Property Value
- JsonSerializerOptions
A JsonSerializerOptions instance that specifies a set of features to support the JsonFormatter object.
SupportedMediaTypes
Gets or sets the collection of MediaTypeHeaderValue elements supported by the JsonFormatter.
public IReadOnlyCollection<MediaTypeHeaderValue> SupportedMediaTypes { get; set; }
Property Value
- IReadOnlyCollection<MediaTypeHeaderValue>
A collection of MediaTypeHeaderValue elements supported by the JsonFormatter.
Methods
ValidateOptions()
Determines whether the public read-write properties of this instance are in a valid state.
public void ValidateOptions()
Remarks
This method is expected to throw exceptions when one or more conditions fails to be in a valid state.
Exceptions
- InvalidOperationException
Settings cannot be null.