convert method

List<int> convert (
  1. Object? object
)
override

Convert object into UTF-8 encoded JSON.

Implementation

List<int> convert(Object? object) {
  var bytes = <List<int>>[];
  // 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) {
      var length = end - start;
      chunk =
          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];
  var length = 0;
  for (var i = 0; i < bytes.length; i++) {
    length += bytes[i].length;
  }
  var result = Uint8List(length);
  for (var 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;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-convert/JsonUtf8Encoder/convert.html