.NET 8 & C#: Best practice: a class sometimes serializes/deserializes as camelCase, other times PascalCase

I currently have a C# class Restaurant like this rudimentary sample:

[JsonPropertyName( "name" )]
public string Name { get; set; } = string.Empty;

[JsonPropertyName( "phoneNumber" )]
public string PhoneNumber { get; set; } = string.Empty;

[JsonPropertyName( "menus" )]
public List<Menu> Menus { get; set; } = [];

The Menu object has PascalCase properties as you would expect in C# - Rules, ItemCategories, etc. However, some APIs want this object in camelCase, and others want it in PascalCase for their own naming consistency.

I’m aware of PropertyNamingPolicy (JSON Serializer options) and how I can serialize this as camelCase, but what about reading it from those APIs when they send it as camelCase and I want to match it to the PascalCase properties? Should I just use PropertyNameCaseInsensitive = true as the option when reading?

Basically I have this one object, and casing may vary when reading/writing. Would you just use these two JsonSerializerOptions I mentioned to allow it to work in all the systems? This seems like a solution but I’m not sure if it’s the ideal solution.

Yes, using PropertyNameCaseInsensitive = true when deserializing is a good and common way to handle JSON with different casing (camelCase or PascalCase) when reading.

For writing (serializing), use PropertyNamingPolicy to control whether you output camelCase or PascalCase depending on the API you call.

So your approach:

  • Deserialize:
    Set PropertyNameCaseInsensitive = true to accept either camelCase or PascalCase JSON input.
  • Serialize:
    Set PropertyNamingPolicy to camelCase or null (for PascalCase) depending on the target API.

This is a clean, flexible, and recommended way to handle varying casing in one object without needing multiple classes or manual mapping.