toJSON

Returns the matrix in the same plain-object shape as toObject, for passing to JSON.stringify.

import { Matrix } from "@teakit/matrix";

Matrix.toJSON({ b: [1], a: [2] }); // { a: [2], b: [1] }  (keys ascending)

// Serialization follows native JSON rules: a row `undefined` becomes `null`.
JSON.stringify(Matrix.toJSON({ a: [1, undefined, 3] }));
// '{"a":[1,null,3]}'

API Reference

Signature

Matrix.toJSON<T, TKey extends string>(
  matrix: Matrix<T, TKey>,
): Matrix<T, TKey>;

Parameters

ParameterTypeRequiredNotes
matrixMatrix<T, TKey>YesA value already known to be a matrix.

Returns

A new plain object with keys ascending and rows copied, suitable for JSON.stringify. A row undefined serializes to null per native rules.

Throws

Does not throw.

Agent Contract

FieldValue
Kindstatic conversion
Canonical nametoJSON
AliasesNone
Mutates inputsNo
ReturnsMatrix<T, TKey>

Agent Notes

  • For a string, call JSON.stringify(Matrix.toJSON(matrix)); the package has no Matrix.toString / Matrix.valueOf.
  • The returned shape matches toObject.