Skip to content

How to make global settings with JsonSerializerOptions whit .net 8.0 #1417

Open

Description

@zdw2018

1

var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};

JsonSerializerOptions.Default = options;
image

2

image
This configuration doesn't work

3

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};

System.Text.Json.JsonSerializer.Serialize(object, option);
It has to be set up this way for it to work, it's a bit too much of a pain to pass a configuration item each time you serialize it

Activity

  1. alenroki commented on Apr 26, 2024

    @alenroki

    I would also like to know how to configure this globally. In Azure Functions before .NET 8 this used to work just fine:

        // Setting global System.Text.Json.JsonSerializerOptions
        services.Configure<JsonSerializerOptions>(options =>
        {
            options.PropertyNameCaseInsensitive = true;
            options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;  // Property is ignored if its value is null. This is applied only to reference-type properties and fields.
        });
    
  2. mswehli commented on Jun 15, 2024

    @mswehli

    After playing around a little i found it. .net 8 seems to have changed where you configure json options from. In addition to the .AddControllers().AddJsonOptions method which is still required for parsing request enums from strings, you should now use the ConfigureHttpJsonOptions method for parsing response enums to strings.

    e.g.

    builder.Services.ConfigureHttpJsonOptions( opt =>{
        opt.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
    });
    
    
  3. viknsagit commented on Jan 23, 2025

    @viknsagit

    None of the options helped.

  4. prashantchoudhary commented on Jan 23, 2025

    @prashantchoudhary

    This is slightly weird as everything else except EnumToJson conversion worked just fine. But the converter had to be explicitly configured? Is it a bug?

    builder.Services.AddControllers().AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    });
    builder.Services.ConfigureHttpJsonOptions(options =>
    {
        options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        options.SerializerOptions.PropertyNameCaseInsensitive = false;
        options.SerializerOptions.AllowTrailingCommas = true;
        options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
    });
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions