title: Conditionally decoding JSON based on a field in the JSON
body: <p>I am receiving JSON from an API and the response can be one of 30 types. Each type has a unique set of fields, but all responses have a field <code>type</code> which states which type it is.</p>
  
  <p>My approach is to use <a href="https://serde-rs.github.io/serde/serde_json/serde_json/" rel="nofollow">serde</a>. I create a struct for each response type and make them decodable. Once I have that how do I choose which struct should be used for a freshly received message?</p>
  
  <p>At the moment, I've created another struct <code>TypeStruct</code> with only a single field for <code>type</code>. I decode the JSON into a <code>TypeStruct</code>, then choose the appropriate struct for received message, based on type value, and decode the message again.</p>
  
  <p>I would like to get rid of this decoding duplication.</p>