README.md (6033B)
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 typed array. 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/array/to-json' ); 41 ``` 42 43 #### toJSON( typedarray ) 44 45 Returns a [JSON][json] representation of a typed array. 46 47 ```javascript 48 var Float64Array = require( '@stdlib/array/float64' ); 49 50 var arr = new Float64Array( [ 5.0, 3.0 ] ); 51 52 var json = toJSON( arr ); 53 /* returns 54 { 55 'type': 'Float64Array', 56 'data': [ 5.0, 3.0 ] 57 } 58 */ 59 ``` 60 61 For guidance on reviving a JSON-serialized typed array, see [`reviver()`][@stdlib/array/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 ## Notes 72 73 - Supported typed array types: 74 75 - [`Float64Array`][@stdlib/array/float64] 76 - [`Float32Array`][@stdlib/array/float32] 77 - [`Int32Array`][@stdlib/array/int32] 78 - [`Uint32Array`][@stdlib/array/uint32] 79 - [`Int16Array`][@stdlib/array/int16] 80 - [`Uint16Array`][@stdlib/array/uint16] 81 - [`Int8Array`][@stdlib/array/int8] 82 - [`Uint8Array`][@stdlib/array/uint8] 83 - [`Uint8ClampedArray`][@stdlib/array/uint8c] 84 85 - The implementation provides basic support for custom typed arrays and sets the `type` field to the closest known typed array type. 86 87 <!-- eslint-disable no-restricted-syntax, no-useless-constructor, new-cap, stdlib/require-globals --> 88 89 ```javascript 90 class CustomArray extends Float64Array() { 91 constructor( data ) { 92 super( data ); 93 } 94 } 95 96 var arr = new CustomArray( [ 5.0, 3.0 ] ); 97 98 var json = toJSON( arr ); 99 /* returns 100 { 101 'type': 'Float64Array', 102 'data': [ 5.0, 3.0 ] 103 } 104 */ 105 ``` 106 107 </section> 108 109 <!-- /.notes --> 110 111 <!-- Package usage examples. --> 112 113 <section class="examples"> 114 115 ## Examples 116 117 <!-- eslint no-undef: "error" --> 118 119 ```javascript 120 var Float64Array = require( '@stdlib/array/float64' ); 121 var Float32Array = require( '@stdlib/array/float32' ); 122 var Int32Array = require( '@stdlib/array/int32' ); 123 var Uint32Array = require( '@stdlib/array/uint32' ); 124 var Int16Array = require( '@stdlib/array/int16' ); 125 var Uint16Array = require( '@stdlib/array/uint16' ); 126 var Int8Array = require( '@stdlib/array/int8' ); 127 var Uint8Array = require( '@stdlib/array/uint8' ); 128 var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); 129 var toJSON = require( '@stdlib/array/to-json' ); 130 131 var arr = new Float64Array( [ 5.0, 3.0 ] ); 132 var json = toJSON( arr ); 133 /* returns 134 { 135 'type': 'Float64Array', 136 'data': [ 5.0, 3.0 ] 137 } 138 */ 139 140 arr = new Float32Array( [ 5.0, -3.0 ] ); 141 json = toJSON( arr ); 142 /* returns 143 { 144 'type': 'Float32Array', 145 'data': [ 5.0, -3.0 ] 146 } 147 */ 148 149 arr = new Int32Array( [ -5, 3 ] ); 150 json = toJSON( arr ); 151 /* returns 152 { 153 'type': 'Int32Array', 154 'data': [ -5, 3 ] 155 } 156 */ 157 158 arr = new Uint32Array( [ 5, 3 ] ); 159 json = toJSON( arr ); 160 /* returns 161 { 162 'type': 'Uint32Array', 163 'data': [ 5, 3 ] 164 } 165 */ 166 167 arr = new Int16Array( [ -5, 3 ] ); 168 json = toJSON( arr ); 169 /* returns 170 { 171 'type': 'Int16Array', 172 'data': [ -5, 3 ] 173 } 174 */ 175 176 arr = new Uint16Array( [ 5, 3 ] ); 177 json = toJSON( arr ); 178 /* returns 179 { 180 'type': 'Uint16Array', 181 'data': [ 5, 3 ] 182 } 183 */ 184 185 arr = new Int8Array( [ -5, 3 ] ); 186 json = toJSON( arr ); 187 /* returns 188 { 189 'type': 'Int8Array', 190 'data': [ -5, 3 ] 191 } 192 */ 193 194 arr = new Uint8Array( [ 5, 3 ] ); 195 json = toJSON( arr ); 196 /* returns 197 { 198 'type': 'Uint8Array', 199 'data': [ 5, 3 ] 200 } 201 */ 202 203 arr = new Uint8ClampedArray( [ 5, 3 ] ); 204 json = toJSON( arr ); 205 /* returns 206 { 207 'type': 'Uint8ClampedArray', 208 'data': [ 5, 3 ] 209 } 210 */ 211 ``` 212 213 </section> 214 215 <!-- /.examples --> 216 217 <!-- 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. --> 218 219 <section class="references"> 220 221 </section> 222 223 <!-- /.references --> 224 225 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 226 227 <section class="links"> 228 229 [json]: http://www.json.org/ 230 231 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array/tree/main/float64 232 233 [@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array/tree/main/float32 234 235 [@stdlib/array/int32]: https://www.npmjs.com/package/@stdlib/array/tree/main/int32 236 237 [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array/tree/main/uint32 238 239 [@stdlib/array/int16]: https://www.npmjs.com/package/@stdlib/array/tree/main/int16 240 241 [@stdlib/array/uint16]: https://www.npmjs.com/package/@stdlib/array/tree/main/uint16 242 243 [@stdlib/array/int8]: https://www.npmjs.com/package/@stdlib/array/tree/main/int8 244 245 [@stdlib/array/uint8]: https://www.npmjs.com/package/@stdlib/array/tree/main/uint8 246 247 [@stdlib/array/uint8c]: https://www.npmjs.com/package/@stdlib/array/tree/main/uint8c 248 249 [@stdlib/array/reviver]: https://www.npmjs.com/package/@stdlib/array/tree/main/reviver 250 251 </section> 252 253 <!-- /.links -->