I recently changed my ASP.NET Core application to target 3.1 from 2.2. I also changed from using Newtonsoft for JSON serialization to System.Text.Json. In the process I have changed a number of response class properties to use JsonPropertyNameAttribute (from the Newtonsoft JsonPropertyAttribute).
However, I have now noticed the application is ignoring the JsonPropertyNameAttribute and serializing the property name to camel case instead.
For example:
[JsonPropertyName("handsets")]
public IEnumerable<GetHandsetResponse> AllHandsets { get; set; }
The response object field is being output as allHandsets in the response and not handsets as specified by the JsonPropertyName.
I'm guessing I'm missing something in the Startup.cs of the application to tell it to use System.Text.Json however I have no idea what. What might I be missing?
It appears that deep down in the common code called from Startup.cs - ConfigureServices() of the application there is a call to .AddNewtonsoftJson() (on the returned IMvcBuilder type). Removing that method call appears to properly revert the application back to using the System.Text.Json serializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With