title: How do I write a Serde Visitor to convert an array of arrays of strings to a Vec<Vec<f64>>?
body: <p>I need to deserialize a JSON into a struct that has a <code>Vec&lt;Vec&lt;f64&gt;&gt;</code> field. The JSON has strings for numbers so I need a custom deserializer to convert the strings to <code>f64</code> during the deserialization.</p>
  
  <p>A sample JSON that I'd like to deserialize:</p>
  
  <pre><code>{
    "values": [["2", "1.4"], ["8.32", "1.5"]]
  }
  </code></pre>
  
  <p>My struct is this:</p>
  
  <pre><code>#[derive(Deserialize)]
  struct Payload {
      #[serde(default, deserialize_with = "from_array_of_arrays_of_strs")]
      values: Vec&lt;Vec&lt;f64&gt;&gt;,
  }
  </code></pre>
  
  <p>I saw you could probably do this with visitors <a href="https://serde.rs/stream-array.html" rel="noreferrer">in the examples of Serde</a>, so I've implemented this visitor:</p>
  
  <pre><code>fn from_array_of_arrays_of_strs&lt;'de, T, D&gt;(deserializer: D) -&gt; Result&lt;Vec&lt;Vec&lt;f64&gt;&gt;, D::Error&gt;
  where
      T: Deserialize&lt;'de&gt;,
      D: Deserializer&lt;'de&gt;,
  {
      struct F64Visitor(PhantomData&lt;fn() -&gt; Vec&lt;Vec&lt;f64&gt;&gt;&gt;);
  
      impl&lt;'de&gt; Visitor&lt;'de&gt; for F64Visitor {
          type Value = Vec&lt;Vec&lt;f64&gt;&gt;;
  
          fn expecting(&amp;self, formatter: &amp;mut fmt::Formatter) -&gt; fmt::Result {
              formatter.write_str("a nonempty sequence of numbers")
          }
  
          #[inline]
          fn visit_str&lt;E&gt;(self, value: &amp;str) -&gt; Result&lt;f64, E&gt;
          where
              E: serde::de::Error,
          {
              self.visit_string(String::from(value))
          }
  
          #[inline]
          fn visit_string&lt;E&gt;(self, value: String) -&gt; Result&lt;f64, E&gt; {
              Ok(value.parse::&lt;f64&gt;().unwrap())
          }
  
          #[inline]
          fn visit_seq&lt;V, T&gt;(self, mut visitor: V) -&gt; Result&lt;Vec&lt;T&gt;, V::Error&gt;
          where
              V: SeqAccess&lt;'de&gt;,
          {
              let mut vec = Vec::new();
  
              while let Some(elem) = try!(visitor.next_element()) {
                  vec.push(elem);
              }
  
              Ok(vec)
          }
      }
  
      let visitor = F64Visitor(PhantomData);
      deserializer.deserialize_seq(visitor)
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=90d5e80118c9a7e799fcdaba3704bb02&amp;version=stable" rel="noreferrer">playground</a></p>
  
  <p>The compiler complains that <code>visit_str</code> and <code>visit_string</code> have an incompatible type for the trait:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0053]: method `visit_str` has an incompatible type for trait
    --&gt; src/main.rs:32:9
     |
  32 | /         fn visit_str&lt;E&gt;(self, value: &amp;str) -&gt; Result&lt;f64, E&gt;
  33 | |             where
  34 | |             E: serde::de::Error,
  35 | |         {
  36 | |             self.visit_string(String::from(value))
  37 | |         }
     | |_________^ expected struct `std::vec::Vec`, found f64
     |
     = note: expected type `fn(from_array_of_arrays_of_strs::F64Visitor, &amp;str) -&gt; std::result::Result&lt;std::vec::Vec&lt;std::vec::Vec&lt;f64&gt;&gt;, E&gt;`
                found type `fn(from_array_of_arrays_of_strs::F64Visitor, &amp;str) -&gt; std::result::Result&lt;f64, E&gt;`
  
  error[E0053]: method `visit_string` has an incompatible type for trait
    --&gt; src/main.rs:40:9
     |
  40 | /         fn visit_string&lt;E&gt;(self, value: String) -&gt; Result&lt;f64, E&gt; {
  41 | |             Ok(value.parse::&lt;f64&gt;().unwrap())
  42 | |         }
     | |_________^ expected struct `std::vec::Vec`, found f64
     |
     = note: expected type `fn(from_array_of_arrays_of_strs::F64Visitor, std::string::String) -&gt; std::result::Result&lt;std::vec::Vec&lt;std::vec::Vec&lt;f64&gt;&gt;, E&gt;`
                found type `fn(from_array_of_arrays_of_strs::F64Visitor, std::string::String) -&gt; std::result::Result&lt;f64, E&gt;`
  
  error[E0049]: method `visit_seq` has 2 type parameters but its trait declaration has 1 type parameter
    --&gt; src/main.rs:45:21
     |
  45 |         fn visit_seq&lt;V, T&gt;(self, mut visitor: V) -&gt; Result&lt;Vec&lt;T&gt;, V::Error&gt;
     |                     ^^^^^^ found 2 type parameters, expected 1
  </code></pre>
  
  <p>I think I don't have the correct understanding of how visitors work. Can I have only one visitor for deserializing the array of arrays of strings, or do I need one visitor for deserializing the arrays and one visitor for deserializing the strings to <code>f64</code>?</p>
  
  <p>I've read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/46753955/how-to-transform-fields-before-deserialization-using-serde">How to transform fields before deserialization using serde?</a></li>
  <li><a href="https://stackoverflow.com/questions/44836327/is-there-is-a-simpler-way-to-convert-a-type-upon-deserialization">Is there is a simpler way to convert a type upon deserialization?</a></li>
  </ul>