title: How do I skip default JavaScript array serialization for IEnumerable types in Json.Net?
body: <p>Some custom types that implement IEnumerable don't necessarily have backing collections. They could be generated dynamically, for example using 'yield' or LINQ. Here is an example:</p>
  <pre><code>public class SOJsonExample
  {
      public class MyCustomEnumerable : IEnumerable&lt;KeyValuePair&lt;int,float&gt;&gt;
      {
          public List&lt;int&gt; Keys { get; set; } = new List&lt;int&gt; { 1, 2, 3 };
          public List&lt;float&gt; Values { get; set; } = new List&lt;float&gt; { 0.1f, 0.2f, 0.3f };
  
          public IEnumerator&lt;KeyValuePair&lt;int, float&gt;&gt; GetEnumerator()
          {
              var kvps =
                  from key in Keys
                  from value in Values
                  select new KeyValuePair&lt;int, float&gt;(key, value);
  
              return kvps.GetEnumerator();
          }
  
          IEnumerator IEnumerable.GetEnumerator() =&gt; this.GetEnumerator();
      }
  }
  </code></pre>
  <p>I have discovered that the default serialization by Json.NET is to enumerate each value and store the   values in a JavaScript array (which I don't want). The default deserializer will then fail to deserialize the collection as it can't be populated. In these cases, I'd instead want Json.NET to skip the default JavaScript array serialization, and just store the members of the class.</p>
  <p>All I want is my keys and values - is there any shortcut way to do this, or do I have to implement my own custom serializer?</p>
  <p>Checked <a href="https://stackoverflow.com/questions/1449142/json-serializing-types-derived-from-ienumerable">this</a> and <a href="https://stackoverflow.com/questions/7567097/json-net-serialization-of-custom-collection-implementing-ienumerablet">this</a>, neither of which are exactly my question. Scanned <a href="http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm" rel="nofollow noreferrer">the documentation</a> as well, but didn't find what I was looking for (perhaps I looked in the wrong place).</p>
  <p>(Edit #1 - improved clarity)</p>
  <p>(Edit #2 - answered my own question...see below)</p>
  <p>(Edit #3 - updated to more modern C#)</p>