I'm writing a client that consumes an API (of which I have no control over). Unfortunately the API's different endpoints are returning inconsistently named JSON fields. For example one endpoint is returning errorCode
but another might return ErrorCode
.
I'm using System.Text.Json
in my .NET Core application for serialization. I would like to do something like the following:
public class MyErrorsResponse
{
[JsonPropertyName("errorCode")]
[JsonPropertyName("ErrorCode")]
public string Code { get; set; }
}
Unfortunately this is not allowed at compile time and I get the error: CS0579 Duplicate 'JsonPropertyName' attribute
.
Is there anyway round this problem? I tried the idea of a 2nd private setter like in: multiple JsonProperty Name assigned to single property. However, it would appear that System.Text.Json
only serializes public fields and ignores private fields.