I am trying to serialize and deserialize an object that contains several buffers, however, deserializing the resulting string from JSON.stringify () with JSON.parse () does not allow the buffers to be recreated correctly.
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);
It gives an output:
source: Buffer parse: Object
This makes sense, since the output from Buffer.toJSON () creates a simple object as follows:
{ type: "Buffer", data: [...] }
I assume that I could cross the resulting object that is looking for sub objects that have the above properties and convert them back to a buffer, however I believe there should be a more elegant solution using JSON.parse () that I miss .
source share