time-to-botec/squiggle/node_modules/@stdlib/buffer/to-json/README.md
NunoSempere b6addc7f05 feat: add the node modules
Necessary in order to clearly see the squiggle hotwiring.
2022-12-03 12:44:49 +00:00

2.8 KiB

toJSON

Return a JSON representation of a Buffer.

Usage

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

toJSON( buffer )

Returns a JSON representation of a Buffer.

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

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

var json = toJSON( 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 randint = require( '@stdlib/random/base/discrete-uniform' );
var toJSON = require( '@stdlib/buffer/to-json' );

var buf;
var i;

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

console.log( toJSON( buf ) );