title: Dynamically ignore property on sealed class when serializing JSON with System.Text.Json
body: <h3>Question</h3>
  <p>Can I dynamically ignore a property from a sealed class using <a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer" rel="noreferrer"><code>System.Text.Json.JsonSerializer</code></a>?</p>
  <h3>Example Code</h3>
  <p><strong>Example class from another library</strong>:</p>
  <pre class="lang-cs prettyprint-override"><code>public sealed class FrozenClass
  {
      // [JsonIgnore] &lt;- cannot apply because I don't own this class
      public int InternalId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  </code></pre>
  <p><strong>Default Serialization</strong>:</p>
  <pre class="lang-cs prettyprint-override"><code>var person = new FrozenClass() { InternalId = 3, FirstName = &quot;Dorothy&quot;, LastName = &quot;Vaughan&quot; };
  var jsonString = System.Text.Json.JsonSerializer.Serialize(person);
  </code></pre>
  <p><strong>Actual Outcome</strong>:</p>
  <pre class="lang-js prettyprint-override"><code>{ &quot;InternalId&quot;: 3, &quot;FirstName&quot;: &quot;Dorothy&quot;, &quot;LastName&quot;: &quot;Vaughan&quot; }
  </code></pre>
  <p><strong>Expected Outcome</strong>:</p>
  <pre class="lang-js prettyprint-override"><code>{ &quot;FirstName&quot;: &quot;Dorothy&quot;, &quot;LastName&quot;: &quot;Vaughan&quot; }
  </code></pre>
  <h3>Alternatives that don't work</h3>
  <p>There are two other questions on how to <a href="https://stackoverflow.com/q/25157511/1366033">add JsonIgnore at RunTime</a> &amp; <a href="https://stackoverflow.com/q/50565358/1366033">add dynamic property name for serialization</a>, but both are targeted at <a href="https://www.newtonsoft.com/json" rel="noreferrer">Newtonsoft's Json.NET</a> - and often reference <a href="https://blog.rsuter.com/advanced-newtonsoft-json-dynamically-rename-or-ignore-properties-without-changing-the-serialized-class/" rel="noreferrer">this extension method</a></p>
  <p>There are several ways to modify the way a class is serialized with the native <code>JsonSerializer</code>, but all seem to rely on modifying the underlying class:</p>
  <ul>
  <li>You can <a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to#exclude-properties-from-serialization" rel="noreferrer">exclude properties from serialization</a> by adding the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonignoreattribute" rel="noreferrer"><code>[JsonIgnore]</code></a> attribute to the original class</li>
  <li>You can <a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to#registration-sample---jsonconverter-on-a-property" rel="noreferrer">register a JsonConverter on a property</a> by adding the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonconverter-1" rel="noreferrer"><code>[JsonConverter]</code></a> attribute to the original class</li>
  </ul>
  <p>However, in my case, the class is from another library and cannot be extended</p>
  <h3>Workarounds</h3>
  <p>A possible workaround is to create another exportable class and setup a mapper between them</p>
  <pre class="lang-cs prettyprint-override"><code>public class MyFrozenClass
  {
      public MyFrozenClass(FrozenClass frozen)
      {
          this.FirstName = frozen.FirstName;
          this.LastName = frozen.LastName;
      }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>var jsonString = System.Text.Json.JsonSerializer.Serialize(new MyFrozenClass(person));
  </code></pre>