title: How can I use JSON.NET to handle a value that is sometimes an object and sometimes an array of the object?
body: <p>I notice there are some other results on stackoverflow for this question but they don't seem to work or are vague. Using the most popular result, I have put together this:</p>
  
  <p>The problem is that when JSON comes back and is being serialised into one of my custom types, one of the bits of JSON is sometimes an array, and sometimes just a string. If my custom type has a string, and the JSON is an array, I get an error. The same happens the other way around, if the JSON is an object and my custom type is an array. It just can't map it to the property.</p>
  
  <p>I decided to solve this, I want to override the deserialisation of this particular property, and if it's an object, I want to convert it into an array of 1 object.</p>
  
  <p>In the object I am serialising to, I added a JsonConverter which I think is going to override the way it's deserialised.</p>
  
  <pre><code>[JsonConverter(typeof(ArrayOrSingleObjectConverter&lt;string&gt;))]
  public List&lt;string&gt; person { get; set; }
  </code></pre>
  
  <p>The idea is that the custom converter will convert a single object to an array. So if the JSON is "Hello" it will set person to be a List containing "Hello" instead of throwing an exception saying cannot convert string to List.</p>
  
  <p>If it's already an array, it should just leave it alone.</p>
  
  <p>The converter looks like this:</p>
  
  <pre><code>public class ArrayOrSingleObjectConverter&lt;T&gt; : JsonConverter
  {
      public override bool CanConvert(Type objectType)
      {
          return true; // Not sure about this but technically it can accept an array or an object, so everything is game.
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          if (objectType == typeof(List&lt;T&gt;))
          {
              return serializer.Deserialize&lt;List&lt;T&gt;&gt;(reader);
          }
          else
          {
              var singleObject = serializer.Deserialize&lt;T&gt;(reader);
              var objectAsList = new List&lt;T&gt;();
              objectAsList.Add(singleObject);
              return objectAsList;
          }
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>It doesn't work. The above code throws an exception trying to deserialize a a single string saying it can't cast it into a List inside the if statement (the 'objectype' is however a List).</p>
  
  <p>I'm struggling to understand exactly what the read and write methods are doing. In the other answer on stackoverflow, it suggests throwing a <code>NotImplementedException</code> in the read method. But if I do that, the read method is called and the exception throws. </p>
  
  <p>I think I'm on the right track, but I need a nudge in the right direction. I think I'm a little confused about what the ReadJSon method is doing and what its parameters mean.</p>
  
  <p>I don't really understand where the value is coming from that it's deserializing since I didn't specify it in the Deserialize method call.</p>
  
  <p>I'm a bit out of my depth on this one.</p>