title: Serialize poco containing object property into Json
body: <p>I have a class that I need to serialize to pass to another system.</p>
  
  <p>The class contains a property that is defined as an object, because the type of class the object will contain can vary at runtime.</p>
  
  <p>My classes looks something like this simplified mock up;</p>
  
  <pre><code>public class MyTestXML
  {
      public string String1 { get; set; }
      public string String2 { get; set; }
  
      [System.Xml.Serialization.XmlElementAttribute("First", typeof(MyFirstObject),
                                Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
      [System.Xml.Serialization.XmlElementAttribute("Second", typeof(MySecondObject),
                                Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
      public object MyObject { get; set; }
  }
  public class MyFirstObject
  {
      public string theFirstObjectString { get; set; }
  }
  public class MySecondObject
  {
      public string theSecondObjectString { get; set; }
  }
  </code></pre>
  
  <p>This class serializes perfectly to xml by using the XmlElementAttribute and XmlSerializer, but when I try and serialize it to Json (using Newtonsoft Json.Net), the object is of an undefined type, and it cannot be deserialized.</p>
  
  <p>Is there a way to specify the XmlElementAttribute in Json attributes to achieve the same result when serialized?</p>
  
  <p>I would like to offer the use of Json for the serialised object, as it is half the size of the xml, but cannot unless I can solve the serialization of the object property issue.</p>
  
  <p>Thanks in advance.</p>