title: Unmarshal a JSON array of heterogeneous structs
body: <p>I want to deserialise an object that includes an array of a some interface <code>Entity</code>:</p>
  
  <pre><code>type Result struct {
      Foo int;
      Bar []Entity;
  };
  </code></pre>
  
  <p><code>Entity</code> is an interface that is implemented by a number of struct types. JSON data identifies the struct type with a "type" field in each entity. E.g.</p>
  
  <pre><code>{"type":"t1","field1":1}
  {"type":"t2","field2":2,"field3":3}
  </code></pre>
  
  <p>How would I go about deserialising the <code>Result</code> type in such a way that it correctly populates the array. From what I can see, I have to:</p>
  
  <ol>
  <li>Implement <code>UnmarshalJSON</code> on <code>Result</code>.</li>
  <li>Parse <code>Bar</code> as a <code>[]*json.RawMessage</code>.</li>
  <li>Parse each raw message as <code>map[string]interface{}</code>.</li>
  <li>Check "type" field in the raw message.</li>
  <li>Create a struct of appropriate type.</li>
  <li>Parse the raw message <em>again</em>, this time into the just created struct.</li>
  </ol>
  
  <p>This all sounds very tedious and boring. Is there a better way to do this? Or am I doing it backwards, and there is a more canonical method to handle an array of heterogeneous objects?</p>