JsonSerializerSettingsExtensions Class
Definition
- Namespace
- Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
- Assemblies
- Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.dll
Extension methods for the Newtonsoft.Json.JsonSerializerSettings class.
public static class JsonSerializerSettingsExtensions
- Inheritance
-
JsonSerializerSettingsExtensions
Examples
API frameworks often define JSON serialization settings in configuration classes but need to propagate those settings to multiple target instances—formatters, exception handlers, response processors—without duplicating configuration logic. Direct property assignment is error-prone and doesn't scale when you have dozens of settings to copy. The Use<T> extension method solves this by copying all serialization properties from a configured source type to a target instance, enabling consistent behavior across your entire request/response pipeline. This example demonstrates how to use the Use<T> method to apply custom JSON serializer settings to an existing JsonSerializerSettings instance:
using System;
using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Cuemon.Configuration;
using Newtonsoft.Json;
namespace Examples;
class CustomSettings : JsonSerializerSettings, IParameterObject
{
public CustomSettings()
{
Formatting = Formatting.Indented;
NullValueHandling = NullValueHandling.Ignore;
}
}
class JsonSerializerSettingsExtensionsExample
{
static void Main()
{
var customSettings = new CustomSettings();
var targetSettings = new JsonSerializerSettings();
// Use the Use<T> method to copy settings from CustomSettings to target
targetSettings.Use<CustomSettings>();
Console.WriteLine($"Target settings formatting: {targetSettings.Formatting}");
Console.WriteLine($"Settings have been synchronized from CustomSettings");
}
}
Methods
| Name | Description |
|---|---|
| Use<T>(JsonSerializerSettings, Action<T>) | Instructs a JSON serializer to propagate the Newtonsoft.Json.JsonSerializerSettings specified by |