title: Unity: Handling inheritance with JSONUtility.ToJSON
body: <p>I'm trying to use the new JSONUtility included with Unity 5.3.2 to serialize an object that contains an object that is the descendant of a base class. Here's an example:</p>
  
  <pre><code>[Serializable]
  public class Class1
  {
      public int class1Field = 1;
      public Class2 attributes = new Class3();
  }
  [Serializable]
  public class Class3 : Class2
  {
      public int class3Field = 3;
  }
  [Serializable]
  public class Class2
  {
      public int class2Field = 2;
  }
  </code></pre>
  
  <p>If I then do:</p>
  
  <pre><code>Class1 myClass = new Class1();
  Debug.Log(JsonUtility.ToJson(myClass));
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>{"class1Field":1,"attributes":{"class2Field":2}}
  </code></pre>
  
  <p>As you can see, nothing from Class3 is being serialized. Interestingly if I do:</p>
  
  <pre><code>Debug.Log(JsonUtility.ToJson(myClass.attributes));
  </code></pre>
  
  <p>The output is :</p>
  
  <pre><code>{"class2Field":2,"class3Field":3}
  </code></pre>
  
  <p>which is what I expect. I know that I can do a pretty simple workaround using string.Format, but is there a more elegant solution to this issue? Am I perhaps not using the JSONUtility correctly? Or is this simply not supported by the JSONUtility at this time?</p>
  
  <p>I'm investigating this functionality as I am using reflection to generate an object (which will come to replace the 'Class3' in this example), and then serialize the top-level object to which the reflected object belongs.</p>
  
  <p>To be clear, my desired output is:</p>
  
  <pre><code>{"class1Field":1,"attributes":{"class2Field":2,"class3Field":3}}
  </code></pre>