title: JSON.NET Serialize as Parent Type
body: <p>I am working on a project where I would like to save the state of an object using human-readable JSON. However, I am running into issues when the originating object of serialization is not the base class. As an example, here is a class and base class:</p>
  <pre><code>public class TeaPot 
  {
      public TeaPot() 
          =&gt; Console.WriteLine(&quot;I want this.&quot;);
  }
  
  public sealed class TeaPotFactory : TeaPot 
  {
      public TeaPotFactory() 
          : base()
          =&gt; Console.WriteLine(&quot;I do not want this!&quot;);
  }
  
  TeaPot greenTea = new TeaPotFactory();
  string json = JsonConvert.SerializeObject(greenTea);
  </code></pre>
  <p>The string <code>json</code> will then contain the following content:</p>
  <pre><code>    {
          &quot;$type&quot;: &quot;Project.Factories.TeaPotFactory, TeaShop&quot;,
          &quot;Name&quot;: &quot;Green Tea&quot;,
          &quot;Price&quot;: 5.99,
      }
  </code></pre>
  <p>This means when the JSON is deserialized the factory constructor will run again causing unintended side-effects. Example console output:</p>
  <pre><code># I want this.
  # I do not want this!
  </code></pre>
  <p>This happens even if I specify the deserialization type as the base class (<code>JsonConvert.DeserializeObject&lt;TeaPot&gt;</code>).</p>
  <p>I need and have purposefully turned on explicit types for the JSON output as there are types that come from the same base implementation.</p>
  <p>How can I get Newtonsoft.Json (JSON.NET) to define the type of the object as its base class? I know with Newtonsoft you can define your own converter, but this would not be an optimal solution as it adds yet another class and a lot of boilerplate code.</p>
  <p>The original code for serialization and deserialization was requested:</p>
  <pre><code>[Singleton]
  public static class Serializer
  {
      public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings()
      {
          TypeNameHandling = TypeNameHandling.Objects,
          TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
          Formatting = Formatting.Indented,
          MissingMemberHandling = MissingMemberHandling.Error,
          NullValueHandling = NullValueHandling.Include
      };
  }
  
  public class Bundle {/* ... */}
  
  public class BundleFactory : Bundle {/* ... */}
  
  public sealed class BundleCollection : ISerializable
  {
      public Bundle[] Bundles 
      { get; set; }
  
      public override string Serialize()
      =&gt; JsonConvert.SerializeObject(this, Serializer.Settings);
  }
  </code></pre>
  <p>Here is a segment of the output when I output BundleCollection:</p>
  <pre><code>{
    &quot;Bundles&quot;: [
      {
        &quot;$type&quot;: &quot;Integration.Factories.BundleFactory, IntegrationService&quot;,
        &quot;Name&quot;: &quot;FisherScientific&quot;,
        &quot;Transporters&quot;: [
          {
            &quot;$type&quot;: &quot;Integration.Transport.LocalSftp, IntegrationProvider&quot;,
            &quot;Directory&quot;: &quot;~/Fisher/&quot;
          }
        ],
        &quot;Parsers&quot;: [
          {
            &quot;$type&quot;: &quot;Integration.Parse.Excel, IntegrationProvider&quot;,
            &quot;Mapping&quot;: {
              &quot;Customer&quot;: &quot;A&quot;,
              &quot;Item&quot;: &quot;I&quot;,
              &quot;Quantity&quot;: &quot;D&quot;,
              &quot;TrackingNo&quot;: &quot;N&quot;
            }
          }
        ]
      }
    ]
  }
  </code></pre>