@stdlib/buffer-to-json

Return a JSON representation of a Buffer.

Usage no npm install needed!

<script type="module">
  import stdlibBufferToJson from 'https://cdn.skypack.dev/@stdlib/buffer-to-json';
</script>

README

toJSON

NPM version Build Status Coverage Status dependencies

Return a JSON representation of a Buffer.

Installation

npm install @stdlib/buffer-to-json

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 ) );

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2021. The Stdlib Authors.