graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Provide a Way to Override JSON Serializer Options

Open RehanSaeed opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. I would like to override the JSON serializer options which seem to be hard coded in HttpResponseExtensions. It would be nice to indent the JSON in dev/test for easier debugging.

Describe the solution you'd like

ASP.NET Core provides a way to override the JSON options e.g. I use this in my Swagger API:

builder.AddJsonOptions(
    options =>
    {
        var jsonSerializerOptions = options.JsonSerializerOptions;
        if (webHostEnvironment.IsDevelopment() ||
            webHostEnvironment.IsEnvironment("Test"))
        {
            // Pretty print the JSON in development/test for easier debugging.
            jsonSerializerOptions.WriteIndented = true;
        }

        jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        jsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
        jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });

RehanSaeed avatar Aug 11 '21 14:08 RehanSaeed

do you have any workaround for this ?

pylvr avatar Apr 19 '22 15:04 pylvr

I see this was reopened. We really need this in place cause of issues with current serialization using System.Text.Json. We need to switch to Newtonsoft.Json but this limitation is preventing us from doing that. Is there an estimate on when this will be implemented?

ikirkpat avatar Sep 27 '22 15:09 ikirkpat

I need this feature. The default JSON serializer seems to be stripping out microseconds from datetimes.

aaron-manning avatar Oct 23 '22 17:10 aaron-manning

This is not really possible as the scalars in GraphQL also define how they translate into JSON or other serialization formats. Also things like casing are very much specified by the GraphQL spec.

However, for version 13 we now allow to specify the indentation options and the JsonEncoder.

services.AddHttpResponseFormatter(indented: true, encoder ....

michaelstaib avatar Oct 24 '22 08:10 michaelstaib