title: Custom JSON de/serializer in C# / Unity
body: <p>I am working in Unity and trying to deserialize JSON.</p>
  
  <p>The JSON was serialized (using Java code) from a data structure that looks like this:</p>
  
  <p>Array of objects of Class A, where</p>
  
  <p>Class A contains an array of objects of Class B, and</p>
  
  <p>Class B contains an array of objects of Class C, and</p>
  
  <p>Class C contains zero or one objects of Class D</p>
  
  <p>The built-in Unity deserializer, JsonUtility, works fine for deserializing this data structure except for Class D, because class D uses polymorphism.  To be more specific, Class D is actually an Interface, and the object that's being deserialized is either of subclass D1 or subclass D2.</p>
  
  <p>Is there a way I can write a custom deserializer for Class D and only for Class D?  I am happy to let the built-in library handle deserializing Classes A through C, but I need to do something special to deserialize Class D.</p>
  
  <p>In Java this was simple because I was able to register a custom deserializer that is called whenever objects of Class D are de/serialized.  Within that deserializer I was able to do some introspection to see what type of subclass we're dealing with (e.g., is it D1 or D2?) and deserialize it appropriately.</p>
  
  <p>I'm not sure how to do this in Unity (or C#).</p>
  
  <p>I have looked into MiniJSON, LitJSON, SimpleJSON, and NewtonSoft's JSON.NET but I haven't found anything in those (maybe I missed it?) that allows you to write a custom deserializer for a class.</p>
  
  <p>The closest I've found is JSON.NET's CustomCreationConvertor:</p>
  
  <p><a href="http://www.newtonsoft.com/json/help/html/CustomCreationConverter.htm" rel="nofollow noreferrer">http://www.newtonsoft.com/json/help/html/CustomCreationConverter.htm</a></p>
  
  <p>which lets you call the constructor for the Class you wish to deserialize into (and the built-in deserializer does the rest) but I'm not sure where I'd be able to do any introspection in order to decide which subclass to construct.</p>
  
  <p>Thanks for any suggestions!</p>
  
  <p>Update: In the comments, Abion47 posted this: <a href="https://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base">How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?</a></p>
  
  <p>That thread shows me how to write the deserializer for Class D.  But a question still remains: how do I invoke the custom deserializer?  In the examples given, the custom deserializer is called like this:</p>
  
  <pre><code>D sampleD = JsonConvert.DeserializeObject&lt;D&gt;(json, new CustomDConverter());
  </code></pre>
  
  <p>The custom deserializer is passed in.  But how can I do this if I am also wanting to use the built-in deserializer for Class C, within which Class D is nested?</p>
  
  <p>Is there any way to tell JSON.NET that I want it to use CustomDConverter any time it encounters an object of Class D (and otherwise, just proceed normally)?</p>