MessagePack is an efficient binary
serialization format. It lets you exchange data among multiple languages
like JSON but it's faster and smaller. For example, small integers (like
flags or error code) are encoded into a single byte, and typical short
strings only require an extra byte in addition to the strings themselves.
If you ever wished to use JSON for convenience (storing an image with
metadata) but could not for technical reasons (binary data, size, speed…),
MessagePack is a perfect replacement.
msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
MessagePack.unpack(msg) #=> [1,2,3]
Use RubyGems to install:
gem install msgpack
or build msgpack-ruby and install:
gem install --local pkg/msgpack
Store objects efficiently serialized by msgpack on memcached or Redis
Upload data in efficient format from mobile devices such as smartphones
Design a portable protocol to communicate with embedded devices
Exchange objects between software components written in different languages
MessagePack for Ruby should run on x86, ARM, PowerPC, SPARC and other CPU
And it works with MRI (CRuby) and Rubinius. Patches to improve portability
is highly welcomed.
Use MessagePack.pack or to_msgpack:
msg = MessagePack.pack(obj) # or
msg = obj.to_msgpack
Packer provides advanced API to serialize objects in streaming style:
# serialize a 2-element array [e1, e2]
pk = MessagePack::Packer.new(io)
reference for details.
obj = MessagePack.unpack(msg)
Unpacker provides advanced API to deserialize objects in streaming style:
# deserialize objects from an IO
u = MessagePack::Unpacker.new(io)
u.each do |obj|
or event-driven style which works well with EventMachine:
# event-driven deserialization
@u ||= MessagePack::Unpacker.new
reference for details.
MessagePack for Ruby provides a buffer API so that you can read or write
data by hand, not via Packer or Unpacker API.
is backed with a fixed-length shared memory pool which is very fast for
small data (<= 4KB), and has zero-copy capability which significantly
affects performance to handle large binary data.
How to build and run tests
Before building msgpack, you need to install bundler and dependencies.
gem install bundler
Then, you can run the tasks as follows:
bundle exec rake build
bundle exec rake spec
bundle exec rake doc
Sadayuki Furuhashi <email@example.com>
Copyright © 2008-2013 Sadayuki Furuhashi
Apache License, Version 2.0