I have some existing code that is working with Buffer objects.
let dataObject = JSON.parse(JSON.stringify(data));
Which seems to me like a no-op, ie it doesn't do anything. That's not correct though, as if I replace it with:
let dataObject = data;
The code fails. I've some some investigation and:
typeof data is objectdata.constructor.name is Object<Buffer>... (long stream of bytes)What does JSON.parse(JSON.stringify(buffer)) do? Is there a better or clearer way of doing this?
The buf.toJSON() method has a misleading name because it returns a JavaScript object, while JSON is actually a string data format. However, it returns a representation of the Buffer and the data it contains, so you can replace this:
let dataObject = JSON.parse(JSON.stringify(data));
With this cleaner code:
let dataObject = data.toJSON();