title: Deserialize JSON to C#: string and string[] values
body: <p>I need to consume JSON from a REST service.  For one of the fields, the service sometimes returns a string for the value yet other times returns an array of strings for the value.</p>
  
  <p>For example:</p>
  
  <pre><code>var jsonArrayResponse = "{ \"id\" : \"abc\", \"relatedIds\" : [ \"def\", \"ghi\", \"jkl\" ] }";
  var jsonSingleResponse = "{ \"id\" : \"123\", \"relatedIds\" : \"456\" }";
  </code></pre>
  
  <p>I have implemented the destination class using the SingleValueArrayConverter pattern found <a href="https://stackoverflow.com/questions/5224697/deserializing-json-when-sometimes-array-and-sometimes-object/28160073#28160073">on this post</a>.  Here is the full set of code to illustrate my problem:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          // this works fine with the SingleValueArrayConverter
          var jsonArray = "{ \"id\" : \"abc\", \"relatedIds\" : [ \"def\", \"ghi\", \"jkl\" ] }";
          var objArray = (MyObject)JsonConvert.DeserializeObject(jsonArray, typeof(MyObject));
  
          // this fails to parse "456" with Exception message:
          //      "Unable to cast object of type 'System.Object' to 
          //       type 'System.Collections.Generic.List`1[System.String]'."
          var jsonSingle = "{ \"id\" : \"123\", \"relatedIds\" : \"456\" }";
          var objSingle = (MyObject)JsonConvert.DeserializeObject(jsonSingle, typeof (MyObject));
      }
  }
  
  [DataContract]
  public class MyObject
  {
      [DataMember(Name = "id")]
      public string Id;
  
      [DataMember(Name = "relatedIds")]
      [JsonConverter(typeof(SingleValueArrayConverter&lt;string&gt;))]
      public List&lt;string&gt; RelatedIds;
  }
  
  public class SingleValueArrayConverter&lt;T&gt; : JsonConverter
  {
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          serializer.Serialize(writer, value);
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          object retVal = new Object();
          if (reader.TokenType == JsonToken.StartObject)
          {
              T instance = (T)serializer.Deserialize(reader, typeof(T));
              retVal = new List&lt;T&gt;() { instance };
          }
          else if (reader.TokenType == JsonToken.StartArray)
          {
              retVal = serializer.Deserialize(reader, objectType);
          }
          return retVal;
      }
  
      public override bool CanConvert(Type objectType)
      {
          return false;
      }
  }
  </code></pre>
  
  <p>How can I fix this to properly parse the two different JSON blobs into the same class?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I modified the SingleValueArrayConverter to check the string case (as mentioned below), and things started working.  I use the converter for more than just strings, so I had to add the first if statement, rather than modify it as suggested).</p>
  
  <pre><code>    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          object retVal = new Object();
  
          if (reader.TokenType == JsonToken.String)
          {
              string instance = (string)serializer.Deserialize(reader);
              retVal = new List&lt;string&gt; () {instance};
          }
  
          else if (reader.TokenType == JsonToken.StartObject)
          {
              T instance = (T)serializer.Deserialize(reader, typeof(T));
              retVal = new List&lt;T&gt;() { instance };
          }
          else if (reader.TokenType == JsonToken.StartArray)
          {
              retVal = serializer.Deserialize(reader, objectType);
          }
          return retVal;
      }
  
      public override bool CanConvert(Type objectType)
      {
          return false;
      }
  }
  </code></pre>