buffer2json

Return a JSON representation of a Buffer.

Usage

var buffer2json = require( '@stdlib/buffer/to-json' );

buffer2json( buffer )

Returns a JSON representation of a Buffer.

var array2buffer = require( '@stdlib/buffer/from-array' );

var buf = array2buffer( [ 1, 2 ] );

var json = buffer2json( buf );
/* returns
    {
        'type': 'Buffer',
        'data': [ 1, 2 ]
    }
*/

For guidance on reviving a JSON-serialized Buffer, see reviver().

Examples

var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
var randi = require( '@stdlib/random/base/discrete-uniform' );
var buffer2json = require( '@stdlib/buffer/to-json' );

var buf = allocUnsafe( 100 );
var i;
for ( i = 0; i < buf.length; i++ ) {
    buf[ i ] = randi( 0, 255 );
}

console.log( buffer2json( buf ) );
Did you find this page helpful?