System.text.json generates camel case properties when serializing objects

If we have an object like this:


public class SomeObject 
{
	public string Prop1 { get; set;}
}

Serializing it with System.text.json or indirectly through HttpClient e.g. yields:


{ "prop1": "..." }

To use the original casing, add this:


JsonSerializerOptions jsonSerializerOptions = new()
{
	PropertyNamingPolicy = null
};

HttpClientHandler httpClientHandler = new()
{
	ServerCertificateCustomValidationCallback = (_, _, _, _) => true
};

using HttpClient httpClient = new(httpClientHandler);

HttpResponseMessage response = await _httpClient.PostAsJsonAsync("https://...some url ....", new
{
	Prop1 = "some value..."
}, jsonSerializerOptions);

Or globally for all controllers:


builder.Services.AddControllers().AddJsonOptions((options) =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});

Post a Comment

Previous Post Next Post