List<int> convert(Object object)

Convert object into UTF-8 encoded JSON.

Source

List<int> convert(Object object) {
  List<List<int>> bytes = [];
  // The `stringify` function always converts into chunks.
  // Collect the chunks into the `bytes` list, then combine them afterwards.
  void addChunk(Uint8List chunk, int start, int end) {
    if (start > 0 || end < chunk.length) {
      int length = end - start;
      chunk = new Uint8List.view(
          chunk.buffer, chunk.offsetInBytes + start, length);
    }
    bytes.add(chunk);
  }

  _JsonUtf8Stringifier.stringify(
      object, _indent, _toEncodable, _bufferSize, addChunk);
  if (bytes.length == 1) return bytes[0];
  int length = 0;
  for (int i = 0; i < bytes.length; i++) {
    length += bytes[i].length;
  }
  Uint8List result = new Uint8List(length);
  for (int i = 0, offset = 0; i < bytes.length; i++) {
    var byteList = bytes[i];
    int end = offset + byteList.length;
    result.setRange(offset, end, byteList);
    offset = end;
  }
  return result;
}