title: JSON.NET TypeNameHandling.Auto except for top level
body: <p>Given the following snippet:</p>
  
  <pre><code>using System;
  using Newtonsoft.Json;
  
  namespace JsonTestje
  {
      class Other
      {
          public string Message2 { get; set; }
      }
  
      class Demo
      {
          public string Message { get; set; }
          public Other Other { get; set; }
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              var demo = new Demo
              {
                  Message = "Hello, World!",
                  Other = new Other
                  {
                      Message2 = "Here be dragons!"
                  }
              };
  
              var settings = new JsonSerializerSettings
              {
                  TypeNameHandling = TypeNameHandling.Auto,
                  Formatting = Formatting.Indented
              };
              var serialized = JsonConvert.SerializeObject(demo, settings);
              Console.WriteLine(serialized);
          }
      }
  }
  </code></pre>
  
  <p>The output is this:</p>
  
  <pre><code>{
    "Message": "Hello, World!",
    "Other": {
      "Message2": "Here be dragons!"
    }
  }
  </code></pre>
  
  <p>Now if I change <code>TypeNameHandling</code> to <code>TypeNameHandling.All</code>, the output becomes this:</p>
  
  <pre><code>{
    "$type": "JsonTestje.Demo, JsonTestje",
    "Message": "Hello, World!",
    "Other": {
      "$type": "JsonTestje.Other, JsonTestje",
      "Message2": "Here be dragons!"
    }
  }
  </code></pre>
  
  <p>But what I want is this:</p>
  
  <pre><code>{
    "$type": "JsonTestje.Demo, JsonTestje",
    "Message": "Hello, World!",
    "Other": {
      "Message2": "Here be dragons!"
    }
  }
  </code></pre>
  
  <p>The reason is that I want to be able to serialize a bunch of objects and deserialize them later, without knowing the type that they deserialize to, but I also don't want to pollute the content with these <code>$type</code> properties all over the place where they're not needed.</p>
  
  <p>In other words, I want <code>TypeNameHandling.Auto</code> except for the root object. How do I do this?</p>