title: Creating Binary Data Structures in Javascript
body: <p>I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:</p>
  
  <pre><code>out.writeShort(session);
  out.writeUnsignedInt(itemID);
  out.writeObject(arbitraryData);
  out.writeShort(score);
  </code></pre>
  
  <p>You would have to write an equivalent function to read bytes from the serialized data and build the class from it.</p>
  
  <p>Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.</p>
  
  <p>I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?</p>
  
  <p>Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.</p>