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
}
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 ?
5 Replies
Angius
Angius3y ago
In .NET 7 you can make those properties required In earlier versions, you can initialize them with = default!;
LazyGuard
LazyGuardOP3y ago
Okay thanks. What about the init ?
public record ProductInformation
{
public ProductDetails Product {get; init;} = default!


public record ProductDetails
{
public Product {get; init;} = default!
public init OwnerId {get; init;} = default!
}
public record ProductInformation
{
public ProductDetails Product {get; init;} = default!


public record ProductDetails
{
public Product {get; init;} = default!
public init OwnerId {get; init;} = default!
}
Now the compiler is suggesting that it's never user and suggest me to refactor to something like public string Procudt => default! but the Deserialization does not work in this case PS: I am using .NET 6
Angius
Angius3y ago
Just ignore it Warnings are just that, warnings Not errors The IDE can't know of every scenario, especially if it involves reflections and stuff. So it just can't know those setters are actually used
LazyGuard
LazyGuardOP3y ago
okay got it, thanks !
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?