Skip to content

Commit ae80da2

Browse files
committed
feat: add ndarray/to-json
1 parent aea44c9 commit ae80da2

File tree

10 files changed

+1198
-0
lines changed

10 files changed

+1198
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
25+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
26+
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
27+
var numel = require( '@stdlib/ndarray/base/numel' );
28+
var zeroTo = require( '@stdlib/array/base/zero-to' );
29+
var ndarray = require( '@stdlib/ndarray/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var ndarray2json = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+':order=row-major', function benchmark( b ) {
37+
var strides;
38+
var buffer;
39+
var offset;
40+
var order;
41+
var shape;
42+
var arr;
43+
var out;
44+
var i;
45+
46+
shape = [ 10, 10, 10 ];
47+
order = 'row-major';
48+
buffer = zeroTo( numel( shape ) );
49+
strides = shape2strides( shape, order );
50+
offset = strides2offset( shape, strides );
51+
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
out = ndarray2json( arr );
56+
if ( typeof out !== 'object' ) {
57+
b.fail( 'should return an object' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isPlainObject( out ) ) {
62+
b.fail( 'should return a JSON object' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+':order=column-major', function benchmark( b ) {
69+
var strides;
70+
var buffer;
71+
var offset;
72+
var order;
73+
var shape;
74+
var arr;
75+
var out;
76+
var i;
77+
78+
shape = [ 10, 10, 10 ];
79+
order = 'column-major';
80+
buffer = zeroTo( numel( shape ) );
81+
strides = shape2strides( shape, order );
82+
offset = strides2offset( shape, strides );
83+
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
out = ndarray2json( arr );
88+
if ( typeof out !== 'object' ) {
89+
b.fail( 'should return an object' );
90+
}
91+
}
92+
b.toc();
93+
if ( !isPlainObject( out ) ) {
94+
b.fail( 'should return a JSON object' );
95+
}
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( x )
3+
Serializes an ndarray as a JSON object.
4+
5+
This function does *not* serialize data outside of the buffer region defined
6+
by the ndarray view.
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input ndarray.
12+
13+
Returns
14+
-------
15+
out: Object
16+
JSON object.
17+
18+
Examples
19+
--------
20+
> var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
21+
> var out = {{alias}}( arr )
22+
{...}
23+
24+
See Also
25+
--------
26+

0 commit comments

Comments
 (0)