Table of Contents

Class JsonSerializerSettingsExtensions

Namespace
Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
Assembly
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

Use<T>(JsonSerializerSettings, Action<T>)

Instructs a JSON serializer to propagate the Newtonsoft.Json.JsonSerializerSettings specified by T on to s1 with an optional setup delegate.

public static void Use<T>(this JsonSerializerSettings s1, Action<T> setup = null) where T : JsonSerializerSettings, IParameterObject, new()

Parameters

s1 JsonSerializerSettings

The Newtonsoft.Json.JsonSerializerSettings to extend.

setup Action<T>

The Newtonsoft.Json.JsonSerializerSettings which need to be configured.

Type Parameters

T

The type of the Newtonsoft.Json.JsonSerializerSettings to use.