title: CustomJsonConverter to deserialize JSON with one field that could be multiple custom objects
body: <p>I am communicating with an API, on various endpoints.  All responses have some shared fields in <code>meta</code>, and then a <code>data</code> object that can contain any number of custom objects.</p>
  
  <p>A few examples: 
  1) A payload returning <code>data</code> containing <code>accounts[]</code></p>
  
  <pre><code>{
  "meta": {
      "timestamp": "2017-07-14T12:59:34-04:00",
      "code": 200
  },
  "data": {
      "accounts": [
          {
              "address": "Main Street",
              "country": "US",
              "email": "1@23.com",
              "city": "MyTown",
              "accountName": "MyAccount",
              "account": 123456,
              "contact": ""
          }
      ]
  }
  }
  </code></pre>
  
  <p>2) A payload returning <code>data</code> containing <code>shipments[]</code></p>
  
  <pre><code>{
  "meta": {
      "timestamp": "2017-07-14T13:46:42-04:00",
      "code": 400
  },
  "data": {
      "shipments": [
          {
              "distributionCenter": "123",
              "packages": [
                  {
                      "packageDetails": {},
                      "consigneeAddress": {},
                      "errors": [
                          {
                              "errorCode": "VALIDATION_ERROR",
                              "errorId": 0,
                              "errorMessage": "INVALID_LOGIN"
                          }
                      ],
              ],
              "status": {
                  "timestamp": "2017-07-14T13:46:42-0400",
                  "numAccepted": 0,
                  "numRejected": 1,
                  "code": "ERROR"
              },
          }
      ]
  }
  }
  </code></pre>
  
  <p>I need to have access to <code>shipments[]</code> when I hit the <code>shipments</code> endpoint, the same with <code>accounts[]</code> when hitting <code>accounts</code> endpoint.  So that <code>data.accounts[0].email</code> is accessible.</p>
  
  <p>The hack way of doing this seems to be creating a <code>Data</code> class that has all possible object types, so when serialization/deserialization happens, those not present are just null.</p>
  
  <pre><code>public class ApiResponse
  {
      public Meta meta { get; set; }
      public Data data { get; set; }
  }
  
  public class Meta
  {
      public string code { get; set; }
      public DateTime timestamp { get; set; }
  }
  
  public class Data
  {
      public Account[] accounts { get; set; }
      public Shipment[] shipments { get; set; }
  }
  </code></pre>
  
  <p>But I am wondering if there is a way to do this with a Newtonsoft JSON custom JsonConverter. Sa <code>Data</code> class looks like:</p>
  
  <pre><code>[JsonConverter(typeof(DataConverter))]
  public class Data
  {
      \\not sure what to put here...
  }
  </code></pre>
  
  <p>Any input on how to get ReadJson to recognize custom objects? Or on other json.net ways of accomplishing this?</p>
  
  <pre><code>class DataConverter : JsonConverter
  {
      public override bool CanConvert(Type objectType)
      {
          return (objectType == typeof(Data));
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          if (reader.Value.GetType() == Shipment)  
          /// this throws error "not valid type in this context"
  
  
          throw new NotImplementedException();
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  }
  }
  </code></pre>