fromMatrixJson function

Matrix? fromMatrixJson(
  1. Map<String, dynamic>? json
)

Restores a matrix instance from given json

Implementation

Matrix? fromMatrixJson(Map<String, dynamic>? json) {
  if (json == null) {
    return null;
  }

  final matrixSource = json[matrixDataJsonKey] as List<dynamic>?;

  if (matrixSource == null) {
    throw Exception('Provided json is missing `$matrixDataJsonKey` field');
  }

  final double2dList = matrixSource
      .map((dynamic row) => (row as List<dynamic>)
          .map((dynamic element) => double.parse(element.toString()))
          .toList(growable: false))
      .toList(growable: false);

  final encodedDType = json[matrixDTypeJsonKey] as String?;

  if (encodedDType == null) {
    throw Exception('Provided json is missing `$matrixDTypeJsonKey` field');
  }

  final dType = fromDTypeJson(encodedDType);

  return Matrix.fromList(double2dList, dtype: dType!);
}