MessagePack: like JSON, but fast and small
Anyone know of an efficient binary format similar to MessagePack that supports deserializing only whitelisted keys, and not paying the penalty of parsing data that isn&#x27;t needed?<p>MessagePack is great, but libraries generally only support deserializing the whole thing. I have an application where these structured documents can be very large, and scanning code sometimes only needs a very small subset of keys.<p>I believe Cap&#x27;n Proto has this feature, but unlike MessagePack it&#x27;s not schemaless.<p>For example, given a struct like this:<p><pre><code>  {
    &quot;id&quot;: &quot;123&quot;,
    &quot;name&quot;: &quot;Developers&quot;,
    &quot;members&quot;: [{
      &quot;id&quot;: &quot;567&quot;,
      &quot;permissions&quot;: [
        {&quot;type&quot;: &quot;read&quot;},
        {&quot;type&quot;: &quot;write&quot;}
      }
    }]
  }
</code></pre>
Let&#x27;s say I only want the name, the ID of each member, and whether permissions.read is set. I may want to do something like (Go):<p><pre><code>  StreamingUnmarshal(b,
    func(keypath string, parse func() interface{}) {
      switch keypath {
        case &quot;id&quot;, &quot;members.id&quot;:
          value := parse()
          &#x2F;&#x2F; ... use value ...
        case &quot;members.permissions.type&quot;:
          if parse().(string) == &quot;read&quot; {
            &#x2F;&#x2F; ...
          }
      }
    })
</code></pre>
Random access could be also work, as long as it didn&#x27;t need to sequentially parse from the beginning of the data to get to the right value each time. Something like:<p><pre><code>  id := GetKey(b, &quot;id&quot;)
  memberIDs := GetKey(b, &quot;members.id&quot;)
  permissionTypes := GetKey(b, &quot;roles.permissions.type&quot;)
</code></pre>
The trickiest bit is treating arrays of nested structs (as in &quot;roles.permissions.read&quot;) correctly, although efficient scanning of keys becomes an important optimization point, too.<p>Probably the best method would be to store the keys pre-sorted at the beginning of the data, so that they&#x27;d better fit in the CPU cache, and have pointers to the offsets of the values:<p><pre><code>  KEY1,KEY2,KEY3,VALUE1,VALUE2,VALUE3
</code></pre>
Arrays of structs are tricky here, again, but this is solvable.