title: .NET Core Serialize Inherited Class Properties -- Not Just Base Properties When Preserving References
body: <p>We have a problem when serializing using System.Text.Json.JsonSerializer.</p>
  <p>In this example, we have three classes: <code>Store</code>, <code>Employee</code>, and <code>Manager</code>.  It is noted that Manager inherits from Employee.</p>
  <pre><code>public class Employee
  {
      public string Name { get; set; }
  
      public int Age { get; set; }
  }
  
  public class Manager : Employee
  {
      public int AllowedPersonalDays { get; set; }
  }
  
  public class Store
  {
      public Employee EmployeeOfTheMonth { get; set; }
  
      public Manager Manager { get; set; }
  
      public string Name { get; set; }
  }
  </code></pre>
  <p>In the class <code>Store</code>, we have a property called <code>EmployeeOfTheMonth</code>.  Well, as an example, suppose this property referenced the same object as the <code>Manager</code> property.  Because <code>EmployeeOfTheMonth</code> is serialized first, it will ONLY serialize the <code>Employee</code> properties.  When serializing the <code>Manager</code> property -- because it is second and the same object -- it will add a reference to the <code>EmployeeOfTheMonth</code>.  When we do this, we're losing the additional property attached to the <code>Manager</code>, which is <code>AllowedPersonalDays</code>.  Additionally, as you can see, it will not deserialize because -- while a Manager is an Employee -- an Employee is not a Manager.</p>
  <p>Here's our short example:</p>
  <pre><code>Manager mgr = new Manager()
  {
      Age = 42,
      AllowedPersonalDays = 14,
      Name = &quot;Jane Doe&quot;,
  };
  
  Store store = new Store()
  {
      EmployeeOfTheMonth = mgr,
      Manager = mgr,
      Name = &quot;ValuMart&quot;
  };
  
  System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions();
  options.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
  
  string serialized = System.Text.Json.JsonSerializer.Serialize&lt;Store&gt;(store, options);
  var deserialized = System.Text.Json.JsonSerializer.Deserialize&lt;Store&gt;(serialized, options); // &lt;-- Will through an exception per reasons stated above
  </code></pre>
  <p>If we look at the variable <code>serialized</code>, this is the content:</p>
  <pre><code>{
    &quot;$id&quot;:&quot;1&quot;,
    &quot;EmployeeOfTheMonth&quot;: {
      &quot;$id&quot;:&quot;2&quot;,
      &quot;Name&quot;:&quot;Jane Doe&quot;,
      &quot;Age&quot;:42
    },
    &quot;Manager&quot;: {
      &quot;$ref&quot;:&quot;2&quot;
    },
    &quot;Name&quot;:&quot;ValuMart&quot;
  }
  </code></pre>
  <p>Using System.Text.Json.JsonSerializer, how can we get the <code>EmployeeOfTheMonth</code> to correctly serialize as a <code>Manager</code>?  That is, we need the serialization to look like the following:</p>
  <pre><code>{
    &quot;$id&quot;:&quot;1&quot;,
    &quot;EmployeeOfTheMonth&quot;: {
      &quot;$id&quot;:&quot;2&quot;,
      &quot;Name&quot;:&quot;Jane Doe&quot;,
      &quot;Age&quot;:42,
      &quot;AllowedPersonalDays&quot;:14         &lt;-- We need to retain this property even if the EmployeeOfTheMonth is a Manager
    },
    &quot;Manager&quot;: {
      &quot;$ref&quot;:&quot;2&quot;
    },
    &quot;Name&quot;:&quot;ValuMart&quot;
  }
  </code></pre>
  <p>I know I can adjust the ORDER of the properties in the <code>Store</code> class, but this is not an option and a very poor choice.  Thank you, all.</p>