सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Dart और Flutter के लिए MessagePack बाइनरी सीरियलाइजेशन फॉर्मेट का एक स्ट्रीमिंग API वास्तुकला। यह नेटवर्क संचार और भंडारण के लिए उपयुक्त प्रभावी, सघन डेटा सीरियलाइजेशन और डीसीरियलाइजेशन की अनुमति देता है। पैकेज सभी मुख्य प्लेटफॉर्मों का समर्थन करता है, जिसमें Android, iOS, Web, macOS, Windows और Linux शामिल हैं। प्रदर्शन के लिए डिज़ाइन किया गया है, यह डेटा को बढ़ते ढंग से प्रोसेस करता है, जिससे मेमोरी ओवरहेड कम होता है। तेज़, कम बैंडविड्थ डेटा आदान-प्रदान की आवश्यकता वाले एप्लिकेशन के लिए आदर्श है।
उपयोग
वास्तविक समय में डेटा हस्तांतरण
संरचित डेटा का कुशल भंडारण
क्रॉस-प्लेटफ़ॉर्म डेटा सिंक्रनाइज़ेशन
उच्च प्रदर्शन वाले API
बाइनरी डेटा आदान-प्रदान
मुख्य विशेषताएँ
कम मेमोरी उपयोग के लिए स्ट्रीमिंग API
संक्षिप्त बाइनरी प्रारूप
क्रॉस-प्लेटफ़ॉर्म समर्थन
त्वरित सीरियलाइज़ेशन / डीसीरियलाइज़ेशन
नेस्टेड डेटा संरचनाओं का समर्थन करता है
इनके लिए उपयुक्त
नेटवर्क आवेदन
डेटा-भारी एप्लिकेशन
कम बैंडविड्थ वातावरण
उच्च थ्रूपुट प्रणाली
मोबाइल और वेब ऐप्स
विचार योग्य बातें
MessagePack फॉर्मेट की समझ आवश्यक है
सीमित मानव पठनीयता
डिबग लॉगिंग के लिए अनुपयुक्त
जटिल डेटा के लिए स्कीमा प्रबंधन की आवश्यकता हो सकती है
बाइनरी फॉर्मेट को डिबगिंग में सावधानीपूर्वक संभालने की आवश्यकता होती है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Streaming API implementation of MessagePack binary serialization format - msgpack.
The basics
Packer and Unpacker classes provide Streaming API for serializing and deserializing data. Unpacker also provide automatic unpacking Map and List by implicitly unpacking internal items.
Streaming API Example - Simple
For the simplest data packaging to bytes packet like this:
final p = Packer();
p.packInt(1); // packet format version
p.packInt(222); // user id
p.packBool(false); // broadcast message to others
p.packString('hi'); // user message textfinal bytes = p.takeBytes(); //Uint8ListyourFunctionSendToServer(bytes); //sends [1, 204, 222, 195, 162, 104, 105]
1 encodes to 1 byte with value 1. 222 encodes to 2 bytes with values [204, 222] because 222 > 127. true encodes to 1 byte with value 195. 'hi' encodes to 3 bytes with first byte containing str length info and other 2 bytes hold symbols values.
For more information, refer to the msgpack documentation.
Streaming packing process not like the way we usually pack data in json, by specifying keys.
The current example is more like how data is packed in TCP/IP frame structure.
Unpacker part
finalList<int> rawBytes = yourFunctionReceiveFromServer(); // receive List<int> bytes from serverfinal u = Unpacker.fromList(rawBytes);
final version = u.unpackInt();
final userId = u.unpackInt();
final broadcast = u.unpackBool();
final message = u.unpackString();
// check values in testexpect(version, equals(1));
expect(userId, equals(222));
expect(broadcast, equals(true));
expect(message, equals('hi'));
Streaming packing and automatic/implicit unpacking example - complex
final p = Packer()
..packListLength(10) //pack 10 different types items to list
..packInt(99)
..packBool(true)
..packString('hi')
..packNull() // explicitly pack null
..packString(null) // implicitly any type can accept null
..packBinary(<int>[104, 105]) // hi codes
..packListLength(2) // pack 2 elements list ['elem1',3.14]
..packString('elem1') // list[0]
..packDouble(3.14) // list[1]
..packString('continue to pack other elements')
..packMapLength(1) // map {'key1':false}
..packString('key1') // map key
..packBool(false) // map value
..packInt(9223372036854775807); // next root list item (map ended)final bytes = p.takeBytes();
final u = Unpacker(bytes);
// Unpack by the same sequential/streaming way // or implicitly/automaticallyfinal l = u.unpackList(); //List<Object>print(l);
// [99, true, hi, null, null, [104, 105], [elem1, 3.14], continue to pack other elements, {key1: false}, 9223372036854775807]
List<Object> items explicitly casted to corresponding types when using.
NOTES
Packing Maps and Lists
firstly, pack Map or List header length
secondly, manually pack all items - that's all
Only need put length header before packing items
After packing all items no need to stop or finish or end this map / list
final list = ['i1','i2'];
final map = {'k1': 11, 'k2': 22};
final p = Packer();
p.packListLength(list.length);
list.forEach(p.packString);
p.packMapLength(map.length);
map.forEach((key, v) {
p.packString(key);
p.packInt(v);
});
final bytes = p.takeBytes();
More examples
More examples can be found in:
test/messagepack_test.dart
example/example.dart
Don't use Packer after calling .takeBytes()
Internally it cleans up underlying _builder, so further behaviour will be useless.
Call .takeBytes() only once. It returns Uint8List of packed bytes.
After that don't continue to use Packer instance (don't call .packXXX() methods and .takeBytes() anymore).
Instead create new Packer instance.
Motivation for creating this package
No other packages available that give streaming API for processing data
Contributing
If you have advice how to improve library code or making it lighter or blazingly faster - don't hesitate to open an issue or pull request!