function toJSON( x ) {
	var out;
	var len;
	var sh;
	var st;
	var o;
	var v;
	var i;

	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. Must provide an ndarray. Value: `%s`.', x ) );
	}
	// Defer to input argument's custom implementation, if already defined...
	if ( hasMethod( x, 'toJSON' ) ) {
		return x.toJSON();
	}
	// Build an object containing all ndarray properties needed to revive a serialized ndarray...
	out = {};
	out.type = 'ndarray';
	out.dtype = getDType( x );
	out.flags = {
		'READONLY': isReadOnly( x )
	};
	out.order = getOrder( x );

	sh = getShape( x );
	out.shape = sh;

	st = getStrides( x );
	out.strides = st.slice();

	o = getOffset( x );

	len = numel( out.shape );

	// Flip the signs of negative strides:
	for ( i = 0; i < st.length; i++ ) {
		if ( out.strides[ i ] < 0 ) {
			out.strides[ i ] *= -1;
		}
	}
	// Cast data to generic array...
	out.data = [];
	if ( isComplexDataType( out.dtype ) ) {
		for ( i = 0; i < len; i++ ) {
			v = x.get.apply( x, ind2sub( sh, st, o, out.order, i, 'throw' ) );
			out.data.push( real( v ), imag( v ) );
		}
	} else {
		for ( i = 0; i < len; i++ ) {
			out.data.push( x.get.apply( x, ind2sub( sh, st, o, out.order, i, 'throw' ) ) );
		}
	}
	return out;
}
Serializes an ndarray as a JSON object.

## Notes

-   The function does **not** serialize data outside of the buffer region defined by the ndarray view.

@param {ndarrayLike} x - input ndarray
@throws {TypeError} must provide an ndarray-like object
@returns {Object} JSON representation

@example
var array = require( '@stdlib/ndarray/array' );

var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>

var o = toJSON( x );
// returns {...}