convert 方法
- Object? object
override
将 object
转换为 UTF-8 编码的 JSON。
实现
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;
}