title: How can I dynamically define the struct for serde_json when the JSON structure is changed without recompiling?
body: <p>For example, we add a structure that is used inside a *.rs script</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Serialize, Deserialize, Debug)]
  struct Foo {
      Output: OUTPUT,
      input: INPUT,
      logs: LOGS,
  }
  
  #[derive(Serialize, Deserialize, Debug)]
  struct OUTPUT {
      width: u32,
      samplerate: u32,
  }
  
  #[derive(Serialize, Deserialize, Debug)]
  struct INPUT {
      fileinputuri: String,
  }
  
  #[derive(Serialize, Deserialize, Debug)]
  struct LOGS {
      statuslog: String,
  }
  </code></pre>
  <p>This is referenced by</p>
  <pre><code>let var: Foo = serde_json::from_str(&amp;contents).expect(&quot;error while reading json&quot;);
  </code></pre>
  <p>When I change the field <code>width: u32,</code> to <code>n_width: u32,</code> in the JSON we would change the struct to</p>
  <pre class="lang-rust prettyprint-override"><code>#[derive(Serialize, Deserialize, Debug)]
  struct OUTPUT {
      n_width: u32,
      samplerate: u32,
  }
  </code></pre>
  <p>Rather than add this to the *.rs file and update it each time, could the struct be extracted to a configuration file and updated and loaded at function call time?</p>
  <p>Are there any safety or performance implications for separating the struct from the *.rs file?</p>
  <p>Is there a better anonymous/unnamed way that changes to the JSON could be made without needing to update the struct?</p>
  <p>Per a comment below, using <code>serde_json::Value</code> is an option, but is this a safe method that can be used in place of a strongly typed structure?  I'm worried about memory safety and security against manipulation by a malicious user that gains access to the JSON file.</p>
  <pre class="lang-rust prettyprint-override"><code>pub enum Value {
      Null,
      Bool(bool),
      Number(Number),
      String(String),
      Array(Vec&lt;Value&gt;),
      Object(Map&lt;String, Value&gt;),
  }
  </code></pre>
  <p>Assume that there is no further reference needed to the <code>n_width</code> field in this program, so no need to open the code by the Rust development team, only the JSON team would make the changes to the JSON file and the struct file.</p>