Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Why would someone use JSON.parse(JSON.stringify()) on a Buffer?

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 object
  • data.constructor.name is Object
  • data when logged is <Buffer>... (long stream of bytes)

What does JSON.parse(JSON.stringify(buffer)) do? Is there a better or clearer way of doing this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda