README.md (2853B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # toJSON 22 23 > Return a [JSON][json] representation of a [Buffer][@stdlib/buffer/ctor]. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var toJSON = require( '@stdlib/buffer/to-json' ); 41 ``` 42 43 #### toJSON( buffer ) 44 45 Returns a [JSON][json] representation of a [`Buffer`][@stdlib/buffer/ctor]. 46 47 ```javascript 48 var array2buffer = require( '@stdlib/buffer/from-array' ); 49 50 var buf = array2buffer( [ 1, 2 ] ); 51 52 var json = toJSON( buf ); 53 /* returns 54 { 55 'type': 'Buffer', 56 'data': [ 1, 2 ] 57 } 58 */ 59 ``` 60 61 For guidance on reviving a JSON-serialized [`Buffer`][@stdlib/buffer/ctor], see [`reviver()`][@stdlib/buffer/reviver]. 62 63 </section> 64 65 <!-- /.usage --> 66 67 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 68 69 <section class="notes"> 70 71 </section> 72 73 <!-- /.notes --> 74 75 <!-- Package usage examples. --> 76 77 <section class="examples"> 78 79 ## Examples 80 81 <!-- eslint no-undef: "error" --> 82 83 ```javascript 84 var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); 85 var randint = require( '@stdlib/random/base/discrete-uniform' ); 86 var toJSON = require( '@stdlib/buffer/to-json' ); 87 88 var buf; 89 var i; 90 91 buf = allocUnsafe( 100 ); 92 for ( i = 0; i < buf.length; i++ ) { 93 buf[ i ] = randint( 0, 255 ); 94 } 95 96 console.log( toJSON( buf ) ); 97 ``` 98 99 </section> 100 101 <!-- /.examples --> 102 103 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 104 105 <section class="references"> 106 107 </section> 108 109 <!-- /.references --> 110 111 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 112 113 <section class="links"> 114 115 [json]: http://www.json.org/ 116 117 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer/tree/main/ctor 118 119 [@stdlib/buffer/reviver]: https://www.npmjs.com/package/@stdlib/buffer/tree/main/reviver 120 121 </section> 122 123 <!-- /.links -->