© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
10 replies
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
}
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"
  }
]
[
  {
    "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)
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 ?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ JsonSerializer: Won't deserialize correctly
C#CC# / help
3y ago
How to deserialize only child object
C#CC# / help
2y ago
✅ How to deserialize as fast as possible?
C#CC# / help
4mo ago