An efficient and schemaless binary format used by the Flutter SDK.
The standard message codec is a binary format, as opposed to text based formats like JSON. Consider the following snippet of JSON:
{
"data": [1, 2, 3, 4],
}
In order for this message to be decoded into a Dart map, a utf8 binary file must first be parsed and validated into a Dart string. Then a second pass is performed which looks for specific characters that indicate JSON structures - for example "{" and "}". No sizes or lengths are known ahead of time while, parsing, so the resulting Dart list created for the "data" key is append to as decoding happens.
In contrast, decoding the standard message codec version of this message avoids utf8 decoding, instead operating on the bytes themselves. The only string constructed will be for the "data" key. The length of the list in the data field is encoded in the structure, meaning the correct length object can be allocated and filled in as decoding happens.
Using standard message codec does not require a schema (like protobuf) or any generated code. This makes it easy to use for dynamic messages and simplifies the integration into existing codebases.
The tradeoff for this ease of use is that it becomes the application's responsibility to verify the structure of messages sent/received. There is also no automatic backwards compatibility like protobuf.
standard_message_codec can be used to encode and decode messages in either Flutter or pure Dart applications.
void main() {
final ByteData? data =
const StandardMessageCodec().encodeMessage(<Object, Object>{
'foo': true,
3: 'fizz',
});
print('The encoded message is $data');
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。