패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.2.1

messagepack

MessagePack 이진 직렬화 형식의 스트리밍 API 구현 - msgpack.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
1.1만
좋아요
40
Pub 점수
150

패키지 설치

flutter pub add messagepack

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

Dart 및 Flutter용 MessagePack 이진 직렬화 형식의 스트리밍 API 구현입니다. 네트워크 통신 및 저장에 적합한 효율적이고 컴팩트한 데이터 직렬화 및 역직렬화를 가능하게 합니다. Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼을 지원합니다. 성능을 고려하여 데이터를 단계적으로 처리하여 메모리 오버헤드를 줄였습니다. 빠르고 저대역폭의 데이터 교환을 필요로 하는 애플리케이션에 이상적입니다.

사용 사례

  • 실시간 데이터 전송
  • 구조화된 데이터의 효율적인 저장
  • 크로스 플랫폼 데이터 동기화
  • 고성능 API
  • 이진 데이터 교환

주요 기능

  • 낮은 메모리 사용을 위한 스트리밍 API
  • 압축된 이진 형식
  • 크로스 플랫폼 지원
  • 빠른 직렬화/역직렬화
  • 중첩된 데이터 구조를 지원합니다

적합한 대상

  • 네트워크 애플리케이션
  • 데이터 집약형 앱
  • 저 대역폭 환경
  • 고처리량 시스템
  • 모바일 및 웹 앱

고려 사항

  • MessagePack 형식에 대한 이해가 필요합니다
  • 제한된 인간의 가독성
  • 디버그 로깅에 적합하지 않습니다
  • 복잡한 데이터에 대해 스키마 관리가 필요할 수 있습니다
  • 이진 형식은 디버깅 시 신중한 처리가 필요합니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

Streaming API implementation of MessagePack binary serialization format - msgpack.

pub package MIT License

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:

-------------------------------------------   
| version | userId | broadcast? | message |
-------------------------------------------
Packer part
import 'package:messagepack/messagepack.dart';
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 text
final bytes = p.takeBytes(); //Uint8List
yourFunctionSendToServer(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
final List<int> rawBytes = yourFunctionReceiveFromServer(); // receive List<int> bytes from server
final u = Unpacker.fromList(rawBytes);
final version = u.unpackInt();
final userId = u.unpackInt();
final broadcast = u.unpackBool();
final message = u.unpackString();

// check values in test
expect(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/automatically
final 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!

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.