-1

According to https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-5.0 CamelCase should be the default formatting for System.Text.Json.

I am however getting PascalCase i.e. first char is a capital.

I tried adding

services.AddControllers().AddJsonOptions(option => option.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase); 

to Startup, which as expected made no difference.

If I add

   var options = new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    };
    return Ok(JsonSerializer.Serialize(collection, options));

to the specific serialiser then I get camelCase i.e. first char lower case.

Anyone else seen this? Am I using it wrong?

KeithT
  • 1
  • 1
  • 1
    Can you share a [mcve] of pascal case getting returned when camel case should have been? E.g. `collection` might contain properties with explicit `[JsonPropertyName]` attributes, or `ExpandoObject` objects that get serialized as dictionaries, we have no idea from what you show. – dbc Jul 02 '21 at 17:58

1 Answers1

0

For net core I could recommend you to install NewtonsoftJson and try this

services.AddControllersWithViews()
//or
services.AddControllers()
    .AddNewtonsoftJson(options =>
           options.SerializerSettings.ContractResolver =
              new CamelCasePropertyNamesContractResolver());
Serge
  • 40,935
  • 4
  • 18
  • 45