title: JSON.NET with Custom Serializer to a custom object
body: <p>So I am attempting to turn my complex object .NET into a totally different object in JSON. Basically, I have an array of objects that could be any type that derives off my base class and I want to turn that array of objects into my own JSON object. I don't think I can just do a simple JsonConvert.Serialize() method call because my JSON object will be structured differently. </p>
  
  <p>So here is a mockup of my .NET classes:</p>
  
  <pre><code>public abstract class A
  {
     public string Id { get; set; }
     public string Title { get; set; }
     public bool Enabled { get; set; }
     public List&lt;X&gt; MyCollection { get; set; }
  }
  
  public class B : A
  {
     public string Foo { get; set; }
  }
  
  public class C : A
  {
     public int Bar { get; set; }
  }
  
  public abstract class X
  {
     public string Id { get; set; }
     public string Title { get; set; }
     public bool Enabled { get; set; }
  }
  
  public class Y : X
  {
     public string Hello { get; set; }
  }
  
  public class Z : X
  {
     public string World { get; set; }
  }
  </code></pre>
  
  <p>Obviously that is a simple view of my real class structure but hopefully some guidance on how to convert this will lend me to be able to convert my real class. Basically, my classes (A,B,C) will contain a list of classes (X,Y,C).</p>
  
  <p>So let's say I have an array/collection of these objects I listed above:</p>
  
  <pre><code>List&lt;A&gt; myObjects = new List&lt;A&gt;();
  A myVar = new B();
  myVar.Title = "Number 1";
  myVar.MyCollection.Add(new Y());
  myVar.MyCollection.Add(new Y());
  myVar.MyCollection.Add(new Z());
  myObjects.Add(myVar);
  
  myVar = new B();
  myVar.Title = "Number 2";
  myVar.MyCollection.Add(new Z());
  myObjects.Add(myVar);
  
  myVar = new C();
  myVar.Title = "Number 3";
  myVar.MyCollection.Add(new Y());
  myVar.MyCollection.Add(new Y());
  myObjects.Add(myVar);
  </code></pre>
  
  <p>I want to take my object 'myObjects' and serialize it into a JSON structure like this:</p>
  
  <pre><code>[
     {
        name: "Number 1", //This is the value of A.Title
        type: "B", //This is a NEW property I want to add to the JSON
        enabled: true, //This is the value of A.Enabled
        foo: "SomeValue", //This is the value of B.Foo
        myCollection: [
           { name: "Y1", type: "Y", enabled: true, hello: "SomeValue" }
           { name: "Y2", type: "Y", enabled: true, hello: "SomeOtherValue" }
           { name: "Z1", type: "Z", enabled: true, world: "SomeValue" }
        ]
     },
     {
        name: "Number 2",
        type: "B",
        enabled: true,
        foo: "SomeValue",
        myCollection: [
           { name: "Z2", type: "Z", enabled: true, world: "SomeValue" }
        ]
     },
     {
        name: "Number 3",
        type: "C",
        enabled: true,
        bar: "SomeValue",
        myCollection: [
           { name: "Y3", type: "Y", enabled: true, hello: "SomeValue" }
           { name: "Y4", type: "Y", enabled: true, hello: "SomeOtherValue" }
        ]
     }
  ]
  </code></pre>
  
  <p>Basically, I want to add my own properties and structure it a little different in JSON than my .NET object reflects. There are some other properties I want to add to my objects as well but they are not listed here (would may this post EVEN bigger). I basically just need a way to take my objects and serialize them in my own custom way. I need to make sure I serialize the derived type so I can carry along some of those properties though. Can anyone help guide me in the right direction on how to solve this problem?</p>