msgpack / msgpack-ruby

MessagePack implementation for Ruby / msgpack.org[Ruby]
http://msgpack.org/
Apache License 2.0
764 stars 117 forks source link

Write msgpack to msgpack #351

Closed kinnalru closed 11 months ago

kinnalru commented 1 year ago

Hello. Cannot find how to write msgpacked data to MessagePack::Packer:

already_packed = {test: 123}.to_msgpack
io = StringIO.new
pk = MessagePack::Packer.new(io)

#how to write already_packed  as some key?
pk.write_map_header(1).write('already').write_bin(already_packed ) # this is not works :(

already_packed used for cached static chunks of data

casperisfine commented 1 year ago

It's a bit of a hack, but you can do this:

require 'msgpack'

already_packed = {test: 123}.to_msgpack
pk = MessagePack::Packer.new
pk.write_map_header(1).write('already')
pk.buffer << already_packed
p MessagePack.load(pk.to_s) # => {"already"=>{"test"=>123}}

But other than that, Packer doesn't expose a way to append a fragment, perhaps it should.

casperisfine commented 1 year ago

To clarify, write_bin, is fro writing a "binary string" as in the "binary string" type in the messagepack format.