convert method
Convert object
into UTF-8 encoded JSON.
Implementation
@override
List<int>? convert(Object object) {
final List<List<int>?> 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) {
Uint8List? chunkTmp;
if (start > 0 || end < chunk.length) {
final int length = end - start;
chunkTmp =
Uint8List.view(chunk.buffer, chunk.offsetInBytes + start, length);
}
bytes.add(chunkTmp);
}
_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;
}
final Uint8List result = Uint8List(length);
for (int i = 0, offset = 0; i < bytes.length; i++) {
final Uint8List byteList = bytes[i] as Uint8List;
final int end = offset + byteList.length;
result.setRange(offset, end, byteList);
offset = end;
}
return result;
}