title: How to deserialize a sequence into a custom type with Serde's SeqDeserializer?
body: <p>I'm trying to write a custom serializer and derializer for a type which is uncomplicated but doesn't have a natural JSON representation.</p>
  <pre><code>pub type ArrayKey = [i16; 5];
  
  pub struct ArrayKeyedMap {
      the_map: HashMap&lt;ArrayKey, u32&gt;
  }
  </code></pre>
  <p>The HashMap, having array keys, can't be represented as a JSON map (which must have string keys). So I want to serialize it as a sequence of pairs. The documentation for <a href="https://serde.rs/impl-serialize.html" rel="nofollow noreferrer">Serialization of a sequence</a> is very clear, and the following works:</p>
  <pre><code>impl Serialize for ArrayKeyedMap {
      fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;
          where
              S: Serializer,
      {
          let mut seq = serializer.serialize_seq(Some(self.the_map.len()))?;
          for (the_key, the_val) in self.the_map.iter() {
              seq.serialize_element(the_key)?;
              seq.serialize_element(the_val)?;
          }
          seq.end()
      }
  }
  </code></pre>
  <p>This serializes as e.g. <code>[[1,2,3,4,5], 1, [5,4,3,2,1], 2]</code>. The documentation for <a href="https://serde.rs/impl-deserialize.html" rel="nofollow noreferrer">deserialization of a sequence</a> is not so easy to follow and I can't make the leap between the Visitor and how to retrieve data from a <code>SeqDeserializer</code>. I'm not sure if I'm meant to pass something in, or get a returned value, or something else.</p>
  <pre><code>impl&lt;'de&gt; Deserialize&lt;'de&gt; for ArrayKeyedMap
  {
      fn deserialize&lt;D&gt;(deserializer: D) -&gt; Result&lt;Self, D::Error&gt;
          where
              D: Deserializer&lt;'de&gt;,
      {
          deserializer.deserialize_seq(SeqDeserializer::new // not sure how to proceed
      }
  }
  </code></pre>
  <p>How do I deserialize this back into the map with array keys?</p>