toJson static method
Serializes a single 2D grid to a JSON-compatible map.
Shape:
{"rows": 3, "cols": 3, "data": "101|010|101"}
rows and cols are inferred from grid. data is the pipe-delimited
string produced by encode, so the payload stays compact even for large
grids.
Example:
import 'dart:convert';
final json = jsonEncode(MatrixData.toJson([[1,0,1],[0,1,0],[1,0,1]]));
// {"rows":3,"cols":3,"data":"101|010|101"}
Implementation
static Map<String, dynamic> toJson(List<List<int>> grid) {
final rows = grid.length;
final cols = rows == 0 ? 0 : grid[0].length;
return {'rows': rows, 'cols': cols, 'data': encode(grid)};
}