0

I'm trying to read a JSON document from a REST web service with Newsoft.JsonNET

The document contains some empty arrays:

{
  "values": [null]
}

This is how a filled array looks like:

{
  "values": ["first", "second", "third"]
}

My model class looks like this:

[DataContract]
public class MyModel
{
   [DataMember]
   public IEnumerable<MyEnum> Values{ get; }

   public MyModel(IEnumerable<MyEnum> values)
   {
      this.Values = values;
   }
}

public MyEnum
{
   First,
   Second,
   Third
}

The null in those empty array causes an ArgumentNullException.

How can I solve this?

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 7
    `[null]` is not an empty array, though; that's array with one element which is the null value; an "empty array" is `[]`; IMO the serializer is doing the right thing here - you could post-process the data to *remove* null items? – Marc Gravell Oct 29 '20 at 14:38
  • [null] and empty [] are not the same thing, for your question you can use [NullValueHandling](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm) – styx Oct 29 '20 at 14:39

0 Answers0