|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# ndarray2json |
| 22 | + |
| 23 | +> Serialize an [ndarray][@stdlib/ndarray/ctor] as a JSON object. |
| 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 ndarray2json = require( '@stdlib/ndarray/to-json' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### ndarray2json( x ) |
| 44 | + |
| 45 | +Serializes an [ndarray][@stdlib/ndarray/ctor] as a JSON object. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 49 | + |
| 50 | +var buffer = [ 1, 2, 3, 4 ]; |
| 51 | +var shape = [ 2, 2 ]; |
| 52 | +var order = 'row-major'; |
| 53 | +var strides = [ 2, 1 ]; |
| 54 | +var offset = 0; |
| 55 | + |
| 56 | +var x = ndarray( 'generic', buffer, shape, strides, offset, order ); |
| 57 | +// returns <ndarray> |
| 58 | + |
| 59 | +var out = ndarray2json( x ); |
| 60 | +// returns { 'type': 'ndarray', 'dtype': 'generic', 'flags': {...}, 'offset': 0, 'order': 'row-major', 'shape': [ 3, 2 ], 'strides': [ 2, 1 ], 'data': [ 1.0, 2.0, 3.0, 4.0 ] } |
| 61 | +``` |
| 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 | +- The function does **not** serialize data outside of the buffer defined by the [ndarray][@stdlib/ndarray/ctor] view. |
| 74 | + |
| 75 | +</section> |
| 76 | + |
| 77 | +<!-- /.notes --> |
| 78 | + |
| 79 | +<!-- Package usage examples. --> |
| 80 | + |
| 81 | +<section class="examples"> |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +<!-- eslint no-undef: "error" --> |
| 86 | + |
| 87 | +```javascript |
| 88 | +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); |
| 89 | +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); |
| 90 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 91 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 92 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 93 | +var ndarray2json = require( '@stdlib/ndarray/to-json' ); |
| 94 | + |
| 95 | +// Create a data buffer: |
| 96 | +var buffer = zeroTo( 27 ); |
| 97 | + |
| 98 | +// Specify array meta data: |
| 99 | +var shape = [ 3, 3, 3 ]; |
| 100 | +var order = 'column-major'; |
| 101 | +var ndims = shape.length; |
| 102 | + |
| 103 | +// Compute array meta data: |
| 104 | +var strides = shape2strides( shape, order ); |
| 105 | +var offset = strides2offset( shape, strides ); |
| 106 | + |
| 107 | +// Print array information: |
| 108 | +console.log( '' ); |
| 109 | +console.log( 'Dims: %s', shape.join( 'x' ) ); |
| 110 | + |
| 111 | +// Randomly flip strides and convert an ndarray to JSON... |
| 112 | +var arr; |
| 113 | +var i; |
| 114 | +for ( i = 0; i < 20; i++ ) { |
| 115 | + strides[ discreteUniform( 0, ndims-1 ) ] *= -1; |
| 116 | + offset = strides2offset( shape, strides ); |
| 117 | + |
| 118 | + console.log( '' ); |
| 119 | + console.log( 'Strides: %s', strides.join( ',' ) ); |
| 120 | + console.log( 'Offset: %d', offset ); |
| 121 | + |
| 122 | + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); |
| 123 | + console.log( JSON.stringify( ndarray2json( arr ) ) ); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.examples --> |
| 130 | + |
| 131 | +<!-- 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. --> |
| 132 | + |
| 133 | +<section class="references"> |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.references --> |
| 138 | + |
| 139 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 140 | + |
| 141 | +<section class="related"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.related --> |
| 146 | + |
| 147 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 148 | + |
| 149 | +<section class="links"> |
| 150 | + |
| 151 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 152 | + |
| 153 | +</section> |
| 154 | + |
| 155 | +<!-- /.links --> |
0 commit comments