|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2021 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// TypeScript Version: 2.0 |
| 20 | + |
| 21 | +/// <reference types="@stdlib/types"/> |
| 22 | + |
| 23 | +import { TypedArray } from '@stdlib/types/array'; |
| 24 | + |
| 25 | +/** |
| 26 | +* JSON representation of typed array. |
| 27 | +*/ |
| 28 | +interface JSONRepresentation { |
| 29 | + /** |
| 30 | + * Typed array type. |
| 31 | + */ |
| 32 | + type: 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray'; // tslint:disable-line:max-line-length |
| 33 | + |
| 34 | + /** |
| 35 | + * Typed array data as a generic array. |
| 36 | + */ |
| 37 | + data: Array<number>; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | +* Returns a JSON representation of a typed array. |
| 42 | +* |
| 43 | +* ## Notes |
| 44 | +* |
| 45 | +* - We build a JSON object representing a typed array similar to how Node.js `Buffer` objects are represented. See [Buffer][1]. |
| 46 | +* |
| 47 | +* [1]: https://nodejs.org/api/buffer.html#buffer_buf_tojson |
| 48 | +* |
| 49 | +* @param arr - typed array to serialize |
| 50 | +* @returns JSON representation |
| 51 | +* |
| 52 | +* @example |
| 53 | +* var Float64Array = require( `@stdlib/array/float64` ); |
| 54 | +* |
| 55 | +* var arr = new Float64Array( [ 5.0, 3.0 ] ); |
| 56 | +* var json = toJSON( arr ); |
| 57 | +* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] } |
| 58 | +*/ |
| 59 | +declare function toJSON( arr: TypedArray ): JSONRepresentation; |
| 60 | + |
| 61 | + |
| 62 | +// EXPORTS // |
| 63 | + |
| 64 | +export = toJSON; |
0 commit comments