title: Templated serialization of C# objects to JSON
body: <p>I need to serialize objects to JSON. I would like to do it with a <strong>template</strong> instead of using data annotations (as most frameworks do). Does anybody know a good way of doing this?</p>
  
  <p>A picture says more than 1000 words. I'm looking for something that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/TQAyW.png" alt="enter image description here"></p>
  
  <p>For example, if I had a class like this:</p>
  
  <pre><code>public class Test  
  { 
      public string Key { get; set; } 
      public string Name { get; set; } 
      public string Code { get; set; } 
      public Test Related { get; set; } 
  } 
  </code></pre>
  
  <p>And a had template string that <em>could</em> look like this:</p>
  
  <pre><code>{ 
      id: "$Key",
      name: "$Name",
      related: "$Related.Name"
  }
  </code></pre>
  
  <p>I want to get a JSON object, whose properties are filled in according to <code>Key</code>, <code>Name</code> and <code>Related.Name</code> of the object.</p>
  
  <p>Basically I'm searching for a <strong>JSON serialization</strong> method that <strong>supports templating</strong> instead.</p>