C#C
C#3y ago
LazyGuard

❔ How to deserialize correctly

Hi, I have the following records :
public record ProductInformation 
{
  public ProductDetails Product {get; init;} // Warnings: Non-nullable property Product is unitialized & auto-property accessor is never user


public record ProductDetails 
{
  public Product {get; init;} // Warnings: Non-nullable property Product is unitialized & auto-property accessor is never user
  public init OwnerId {get; init;} //auto-property accessor is never user
}

this is a little bit weird (the name Product is used twice), because unfortunately the records are used to deserialize objects returned from an external API that returns a list either empty or have the following format:
[
  {
    "product": {
      "ownerId": "39",
      "product": "1_4873jm_jd"
  }
]


To deserialize, I am using
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 
if (string.IsNullOrEmpty(responseString))
  return null;
var products = JsonSerializer.Deserialize<IReadOnlyCollection<ProductInformation>>(responseString)


The problem is that I don't know how to do this properly in order not to have the warnings. Any idea ?
Was this page helpful?