ASP.NET Core 3.1: Default System.Text.Json Settings Don't Roundtrip (Serialize/Deserialize) Through Test Server

Written by Ken Dale
This post is days old.

We’ve spotted some strange behavior before with ASP.NET Core and JSON serialization/deserialization, and I eventually made it back to trying out some scenarios.

One thing I’ve noticed is the default System.Text.Json settings don’t roundtrip (that is, serialize and deserialize) through the ASP.NET Core 3.1 TestServer (without additional configuration).

The underlying issue is by default there’s a character case mismatch.

Making It Work

One way to make it work is setting the property names case insensitive when deserializing the response:

var responseObj = JsonSerializer.Deserialize<TestObject>(strContent, new JsonSerializerOptions
    // Required for test to pass, by default
    // there's a case mismatch that isn't automatically handled
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    // or
    // PropertyNameCaseInsensitive = true

Alternatively, we could update the character case to match expectations via an attribute ([JsonPropertyName("StringValue")]) on each property.

In Closing

A test associated with this post is at

Suggested reading