Code Monkey home page Code Monkey logo

msgpack-ruby's Introduction

MessagePack

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.

require 'msgpack'
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:

bundle
rake
gem install --local pkg/msgpack

Use cases

  • Create REST API returing MessagePack using Rails + [RABL](github.com/nesquena/rabl)

  • Store objects efficiently serialized by msgpack on memcached or Redis

  • Upload data in efficient format from mobile devices such as smartphones

    • MessagePack works on iPhone/iPad and Android. See also Objective-C and Java implementations

  • Design a portable protocol to communicate with embedded devices

    • Check also Fluentd which is a log collector which uses msgpack for the log format (they say it uses JSON but actually it’s msgpack, which is compatible with JSON)

  • Exchange objects between software components written in different languages

    • You’ll need a flexible but efficient format so that components exchange objects while keeping compatibility

Portability

MessagePack for Ruby should run on x86, ARM, PowerPC, SPARC and other CPU architectures.

And it works with MRI (CRuby) and Rubinius. Patches to improve portability is highly welcomed.

Serializing objects

Use MessagePack.pack or to_msgpack:

require 'msgpack'
msg = MessagePack.pack(obj)  # or
msg = obj.to_msgpack

Streaming serialization

Packer provides advanced API to serialize objects in streaming style:

# serialize a 2-element array [e1, e2]
pk = MessagePack::Packer.new(io)
pk.write_array_header(2).write(e1).write(e2).flush

See API reference for details.

Deserializing objects

Use MessagePack.unpack:

require 'msgpack'
obj = MessagePack.unpack(msg)

Streaming deserialization

Unpacker provides advanced API to deserialize objects in streaming style:

# deserialize objects from an IO
u = MessagePack::Unpacker.new(io)
u.each do |obj|
  # ...
end

or event-driven style which works well with EventMachine:

# event-driven deserialization
def on_read(data)
  @u ||= MessagePack::Unpacker.new
  @u.feed_each(data) {|obj|
     # ...
  }
end

See API reference for details.

Extension Types

Packer and Unpacker support Extension types of MessagePack.

# register how to serialize custom class at first
pk = MessagePack::Packer.new(io)
pk.register_type(0x01, MyClass1, :to_msgpack_ext) # equal to pk.register_type(0x01, MyClass)
pk.register_type(0x02, MyClass2){|obj| obj.how_to_serialize() } # blocks also available

# almost same API for unpacker
uk = MessagePack::Unpacker.new()
uk.register_type(0x01, MyClass1, :from_msgpack_ext)
uk.register_type(0x02){|data| MyClass2.create_from_serialized_data(data) }

MessagePack::Factory is to create packer and unpacker which have same extention types.

factory = MessagePack::Factory.new
factory.register_type(0x01, MyClass1) # same with next line
factory.register_type(0x01, MyClass1, packer: :to_msgpack_ext, unpacker: :from_msgpack_ext)
pk = factory.packer(options_for_packer)
uk = factory.unpacker(options_for_unpacker)

For MessagePack.pack and MessagePack.unpack, default packer/unpacker refer MessagePack::DefaultFactory. Call MessagePack::DefaultFactory.register_type to enable types process globally.

MessagePack::DefaultFactory.register_type(0x03, MyClass3)
MessagePack.unpack(data_with_ext_typeid_03) #=> MyClass3 instance

Buffer API

MessagePack for Ruby provides a buffer API so that you can read or write data by hand, not via Packer or Unpacker API.

This MessagePack::Buffer 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
bundle install

Then, you can run the tasks as follows:

  • Build

    bundle exec rake build
    
  • Run tests

    bundle exec rake spec
    
  • Generating docs

    bundle exec rake doc
    

How to build -mingw32 rubygems

MessagePack mingw32/64 rubygems build process uses rake-compiler-dock. Run:

rake build:windows

Once this step successes, target gems exist in pkg/msgpack-*-{x86,x64}-mingw32.gem.

Updating documents

Online documents (ruby.msgpack.org) is generated from gh-pages branch. Following commands update documents in gh-pages branch:

bundle exec rake doc
git checkout gh-pages
cp doc/* ./ -a

Copyright

Author

Sadayuki Furuhashi <[email protected]>

Copyright

Copyright © 2008-2015 Sadayuki Furuhashi

License

Apache License, Version 2.0

msgpack-ruby's People

Contributors

frsyuki avatar tagomoris avatar gfx avatar tokuhirom avatar muga avatar methane avatar tanakh avatar iconara avatar makamaka avatar grddev avatar kazuki avatar kzk avatar moriyoshi avatar advect avatar koichiro avatar funny-falcon avatar dgryski avatar mattheworiordan avatar xerial avatar kou avatar repeatedly avatar cosmo0920 avatar firewood avatar hayamiz avatar larskanis avatar dbussink avatar mzp avatar matugm avatar kosaki avatar watabiki avatar

Watchers

Fumihiro Ito avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.