How to deserialize a nested Buffer using JSON.parse
<p>I am trying to serialise and deserialise an object which contains multiple Buffers, however deserialising the resulting string from JSON.stringify() with JSON.parse() fails to correctly re-create the Buffers.</p>

<pre><code>var b64 = 'Jw8mm8h+agVwgI/yN1egchSax0WLWXSEVP0umVvv5zM='; 
var buf = new Buffer(b64, 'base64');

var source = {
    a: {
        buffer: buf
    }
};

var stringify = JSON.stringify(source);
var parse = JSON.parse(stringify);

console.log("source: "+source.a.buffer.constructor.name);
console.log("parse: "+parse.a.buffer.constructor.name);
</code></pre>

<p>Gives the ouput:</p>

<pre><code>source: Buffer
parse: Object
</code></pre>

<p>This makes sense since the output from Buffer.toJSON() creates a simple object like so:</p>

<pre><code>{
  type: "Buffer",
  data: [...]
}
</code></pre>

<p>I guess I could traverse the resulting object looking for sub objects that have the above properties and convert them back to a Buffer, however I feel there should be a more elegant solution using JSON.parse() that I am missing.</p>