Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created May 25, 2016 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tagomoris/0395714af06cb7a519adbaaec10002de to your computer and use it in GitHub Desktop.
Save tagomoris/0395714af06cb7a519adbaaec10002de to your computer and use it in GitHub Desktop.
require 'msgpack'
class MyObj
def initialize(sec=nil, nsec=nil)
t = Time.now
@sec = sec || t.sec
@nsec = nsec || t.nsec
end
def to_msgpack(io = nil)
@sec.to_msgpack(io)
end
def to_msgpack_ext
[@sec, @nsec].pack('NN')
end
def self.from_msgpack_ext(data)
new(*data.unpack('NN'))
end
end
factory = MessagePack::Factory.new
factory.register_type(0x01, MyObj)
obj = MyObj.new
packer = factory.packer
p 123.to_msgpack(packer)
# p packer.write(obj).to_s
# hash = {"tag" => "test", "obj" => obj}
# p packer.write(hash).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment