If you're using JSON.NET, try making the property read-only:
public class PBS
{
public ICollection<int> MyItems { get; } = new List<int>();
}
JSON.NET handles that correctly:
{}
⇒ {"MyItems":[]}
;{"MyItems":null}
⇒ {"MyItems":[]}
;{"MyItems":[1,2,3]}
⇒ {"MyItems":[1,2,3]}
;
NB: Unfortunately,
the new System.Text.Json
APIs[
^] can't currently cope with this - the
MyItems
collection will always be empty. There is
a proposal to add a new attribute[
^] to work around the problem.