title: JSON.Net - How to deserialize a nested field of a custom type
body: <p>I've been having trouble finding a question about my particular case:</p>
  <p>I need to deserialize the following JSON:</p>
  <pre><code>{ 
      &quot;name&quot;: &quot;My Farm&quot;,
      &quot;barns&quot;: [
          {
              &quot;name&quot;&quot;: &quot;Barn A&quot;&quot;,
              &quot;animalTypes&quot;: [
                  &quot;Cow&quot;,
                  &quot;Goat&quot;
              ]
          }
      ]
  }
  </code></pre>
  <p>to the following code model:</p>
  <pre><code>public class Farm
  {
      public string name;
      public Barn[] barns;
  }
  
  public class Barn
  {
      public string name;
      public AnimalType[] animalTypes;
  }
  
  public class AnimalType
  {
      public int typeID;
  }
  </code></pre>
  <p>The problem: I need to deserialize a JSON string (which described an animal species name) into an 'AnimalType' object which contains a &quot;type ID&quot; int. This requirement is not something I can change.</p>
  <p>To get the animal's integer type ID, I have access to an externally supplied &quot;AnimalTypeResolver&quot; class, which looks like this:</p>
  <pre><code>public class AnimalTypeResolver
  {
      public int GetAnimalType(string animalTypeName)
      {
          // queries a map to return the right ID.
      }
  }
  </code></pre>
  <p>I can query that resolver to get the int value I need to store for each animal type.</p>
  <p>So, I tried to write a custom JSONConverter for AnimalType:</p>
  <pre><code>public class AnimalTypeConverter : JsonConverter&lt;AnimalType&gt;
  {
      public AnimalTypeResolver animalTypeResolver;
  
      public AnimalTypeConverter(AnimalTypeResolver animalTypeResolver)
      {
          this.animalTypeResolver = animalTypeResolver;
      }
  
      public override bool CanWrite =&gt; false;
  
      public override AnimalType ReadJson(ref JsonReader reader, Type objectType, AnimalType existingValue, bool hasExistingValue, JsonSerializer serializer)
      {
          if (reader.TokenType == JsonToken.String)
          {
              string animalTypeName = (string)reader.Value;
  
              return new AnimalType
              {
                  typeID = animalTypeResolver.GetAnimalType(animalTypeName)
              };
          }
  
          return null;
      }
  
      public override void WriteJson(JsonWriter writer, AnimalType value, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>And my deserialization code looks like this:</p>
  <pre><code>string farmJSON =
  @&quot;{ 
      &quot;&quot;name&quot;&quot;: &quot;&quot;My Farm&quot;&quot;,
      &quot;&quot;barns&quot;&quot;: [
          {
              &quot;&quot;name&quot;&quot;: &quot;&quot;Barn A&quot;&quot;,
              &quot;&quot;animalTypes&quot;&quot;: [
                  &quot;&quot;Cow&quot;&quot;,
                  &quot;&quot;Goat&quot;&quot;
              ]
          }
      ]
  }&quot;;
  
  JsonSerializer serializer = new JsonSerializer();
  serializer.Converters.Add(new AnimalTypeConverter(animalTypeResolver)); // this animalTypeResolver is supplied from elsewhere in code.
  
  Farm farm = JsonConvert.DeserializeObject&lt;Farm&gt;(farmJSON);
  </code></pre>
  <p>But I get a runtime error:</p>
  <blockquote>
  <p>ArgumentException: Could not cast or convert from System.String to AnimalType.</p>
  </blockquote>
  <p>From reading similar questions, I believe my problem is that I'm trying to deserialize a nested field of a custom type (AnimalType[]). Other answers have explained that JToken.FromObject() creates a new JsonSerializer for each level of deserialization, which has no concept of the JsonConverters I added to a higher-level serializer.</p>
  <p>However, For one reason or another, the other questions on this site have answers which aren't exactly applicable to my case.</p>
  <p>How I can use my custom JsonConverter, to handle a case in which the data is deeply nested?</p>
  <p>If anyone can offer advice about how to make this work, thank you!</p>