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.