I'm parsing some JSON data I receive from a server using the built-in System.Text.Json
module.
Here's an example class that I would use:
public class Something
{
[JsonPropertyName("items")]
public Item[] Items { get; set; }
}
The JSON data for this is usually received like the following, and it's properly parsed with JsonSerializer.Deserialize<Something>()
:
{
"items": [ { ... }, { ... }, { ... } ]
}
However, when there's no items, the server instead returns an empty object, which causes an exception because it expected an array.
{
"items": {}
}
Is there any way I could set it so that an empty object would be considered as an empty array? I've seen that you can make a custom JSON converter but I struggled to get it working.