msgpack android,GitHub - msgpack/msgpack-ruby: MessagePack implementation for Ruby / msgpack.org[Rub...

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

Store objects efficiently serialized by msgpack on memcached or Redis

In fact Redis supports msgpack in EVAL-scripts

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.

Serializing and deserializing symbols

By default, symbols are serialized as strings:

packed = :symbol.to_msgpack # => "\xA6symbol"

MessagePack.unpack(packed) # => "symbol"

This can be customized by registering an extension type for them:

MessagePack::DefaultFactory.register_type(0x00, Symbol)

# symbols now survive round trips

packed = :symbol.to_msgpack # => "\xc7\x06\x00symbol"

MessagePack.unpack(packed) # => :symbol

The extension type for symbols is configurable like any other extension type.

For example, to customize how symbols are packed you can just redefine

Symbol#to_msgpack_ext. Doing this gives you an option to prevent symbols from

being serialized altogether by throwing an exception:

class Symbol

def to_msgpack_ext

raise "Serialization of symbols prohibited"

end

end

MessagePack::DefaultFactory.register_type(0x00, Symbol)

[1, :symbol, 'string'].to_msgpack # => RuntimeError: Serialization of symbols prohibited

Serializing and deserializing Time instances

There are the timestamp extension type in MessagePack,

but it is not registered by default.

To map Ruby's Time to MessagePack's timestamp for the default factory:

MessagePack::DefaultFactory.register_type(

MessagePack::Timestamp::TYPE, # or just -1

Time,

packer: MessagePack::Time::Packer,

unpacker: MessagePack::Time::Unpacker

)

See API reference for details.

Extension Types

# 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 extension 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 -java rubygems

To build -java gems for JRuby, run:

rake build:java

If this directory has Gemfile.lock (generated with MRI), remove it beforehand.

Updating documents

Online documents (http://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 frsyuki@gmail.com

Copyright

Copyright (c) 2008-2015 Sadayuki Furuhashi

License

Apache License, Version 2.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object.hpp:664:34: error: ‘void* memcpy(void*, const void*, size_t)’ copying an object of non-trivial type ‘struct msgpack::v2::object’ from an array of ‘const msgpack_object’ {aka ‘const struct msgpack_object’} [-Werror=class-memaccess] std::memcpy(&o, &v, sizeof(v)); ^ In file included from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_fwd.hpp:17, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/adaptor/adaptor_base_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base_decl.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object_decl.hpp:16, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack.hpp:10, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/example/cpp03/stream.cpp:10: /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v2/object_fwd.hpp:23:8: note: ‘struct msgpack::v2::object’ declared here struct object : v1::object { ^~~~~~ cc1plus: all warnings being treated as errors make[2]: *** [example/cpp03/CMakeFiles/stream.dir/build.make:63:example/cpp03/CMakeFiles/stream.dir/stream.cpp.o] 错误 1 make[2]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master” make[1]: *** [CMakeFiles/Makefile2:415:example/cpp03/CMakeFiles/stream.dir/all] 错误 2 make[1]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master”
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值