title: Customizing Json.NET serialization: turning object into array to avoid repetition of property names
body: <p>I'm sending large amounts of different JSON graphs from a server to a client (I control both) and they all contain a pathological case: a large array of homogeneous (same type) values. So, for example, part of the payload looks like:</p>
  
  <pre><code>[{"LongPropertyName":87, "AnotherVeryLongPropertyName":93,
    "BlahBlahBlahBlahBlah": 78},
   {"LongPropertyName":97, "AnotherVeryLongPropertyName":43,
    "BlahBlahBlahBlahBlah": 578},
   {"LongPropertyName":92, "AnotherVeryLongPropertyName":-3,
    "BlahBlahBlahBlahBlah": 817}, ...
  </code></pre>
  
  <p>I've added some formatting, but as you can see, it's ridiculous from a <a href="http://en.wikipedia.org/wiki/Huffman_coding">Huffman coding</a> point of view, ie that common things should be efficiently expressed.</p>
  
  <p>So, since I control both the deserialization and the serialization ends, I want to implement a transform where this:</p>
  
  <pre><code>[{"Key1":87,"Key2":99},{"Key1":42,"Key2":-8}]
  </code></pre>
  
  <p>gets turned into something like this:</p>
  
  <pre><code>[["$","Key1","Key2"],[87,99],[42,-8]]
  </code></pre>
  
  <p>which as you can see is more compact even with just two objects.</p>
  
  <p><strong>Where do I hook into Json.NET to do this transformation?</strong> I want to do this automatically for as many objects as possible. I've found <a href="http://james.newtonking.com/json/help/index.html?topic=html/ContractResolver.htm">ContractResolvers</a> but I'm not sure if they're happening at the stage I want - I'm not sure how to use its methods to turn a JSON object/dictionary into an array. </p>
  
  <p>Alternatively, if a similar thing has already been implemented for Json.NET, I'd want to use that instead. But I am not confused about the sort of change I want to make (see above), just where I'd hook into Json.NET to make it happen.</p>
  
  <p>(I have tried gzipping it. It works fine and shaves off between 70% and 95%, but it still has to output the full JSON text and do all that compression/decompression. This question is: how do I just output a more compact form of the data from the beginning?)</p>
  
  <hr>
  
  <p><strong>Update:</strong> The way you do this is with a <code>JsonConverter</code>. I had already written several but for some reason I thought they would conflict.</p>
  
  <p>What I ended up with was Brian Rogers' base <a href="http://pastebin.com/1H3hv133">along with some changes to also embed/flatten any directly contained objects</a>. This was not part of the original question, but the reason I did that is because if I had:</p>
  
  <pre><code>[{"A": 42,"B":{"PropOne":87,"PropTwo":93,"PropThree":78}},
  {"A":-72,"B":{"PropOne":97,"PropTwo":43,"PropThree":578}]
  </code></pre>
  
  <p>...I ended up with:</p>
  
  <pre><code>[["A","B"],[42,{"PropOne":87,"PropTwo":93,"PropThree":78}],
  [-72,{"PropOne":97,"PropTwo":43,"PropThree":578}]]
  </code></pre>
  
  <p>...and that doesn't really save anything. Whereas if I embedded/flattened the object as its constituent keys, I end up with:</p>
  
  <pre><code>[["A","B_PropOne","B_PropTwo","B_PropThree"],[42,87,93,78],[-72,97,43,578]]
  </code></pre>