Skip to content

Latest commit

 

History

History
51 lines (42 loc) · 2.33 KB

custom-json-response-serialization-settings.md

File metadata and controls

51 lines (42 loc) · 2.33 KB

Custom JSON response serialization settings

Available starting with v1.8.0

By default each response is serialized using Json.Net and serialization settings (JsonSerializerSettings) are determined by the requested response casing. If the requested casing is camel casing, the default, the folowing serialization settings are applied to the response:

var settings = new JsonSerializerSettings()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

snippet source | anchor

If the requested case is pascal, the following settings are applied:

var settings = new JsonSerializerSettings();

snippet source | anchor

It's possible to customize the response serialization settings on a case-by-case using the following configuration:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();
    services.AddViewModelComposition(options =>
    {
        options.ResponseSerialization.UseCustomJsonSerializerSettings(request =>
        {
            return new JsonSerializerSettings();
        });
    });
}

snippet source | anchor

Each time ServiceComposer needs to serialize a response it'll invoke the supplied function.

NOTE: When customizing the serialization settings, it's responsibility of the function to configure the correct resolver for the requested casing