Writing JSONValue methods with nested EJSON types?

I have two custom types, Boardand Tile. Both must be EJSONcompatible. However, it Boardcontains a two-dimensional array Tiles:

function Board(size) {
  this.tiles = [];
  for (var row = 0; row < size; row++) {
    this.tiles[row] = [];
    for (var col = 0; col < size; col++) {
      this.tiles[row][col] = new Tile(row, col);
    }
  }
  /* ... */
}

My question is about the method toJSONValueon Board. Is it necessary to explicitly convert nested ones Tileas follows:

Board.prototype.toJSONValue = function() {
  var value = {};
  value.tiles = _.map(this.tiles, function(row) {
    return _.map(row, function(tile) {
      return tile.toJSONValue();
    });
  });
  return value;
};

Or the nested type will be automatically converted EJSON, allowing me to do something like this:

Board.prototype.toJSONValue = function() {
  return { tiles: this.tiles };
};

, Tile , EJSON, . , , toJSONValue. , EJSON?

+4
1

JSON.stringify , . , JavaScript, :

JavaScript console screenshot

, , , . ?

. ; EJSON, JSON- ( , ..). JSON- , .

0

All Articles