Output serialized objects to ofstream:
#include <msgpack.hpp>
ofstream out("output.txt");
msgpack::packer<std::ofstream> pk(&out);
//packing an integer
int n=10;
pk.pack(n);
//packing a integer list
vector<int> num_list;
num_list.push_back(1); num_list.push_back(2); num_list.push_back(3);
pk.pack(num_list);
//packing a integer list one by one
pk.pack_array(num_list.size());
for(int j = 0;j<num_list.size();j++){
pk.pack(num_list[i]);
}
Input serialized object from ifstream:
ifstream in("input.txt",ios::binary);
stringstream buffer;
buffer << in.rdbuf();
msgpack::unpacker pac;
// feeds the buffer.
pac.reserve_buffer(buffer.str().size());
memcpy(pac.buffer(), buffer.str().data(), buffer.str().size());
pac.buffer_consumed(buffer.str().size());
// now starts streaming deserialization.
msgpack::object_handle oh;
while(pac.next(oh)) {
std::cout << oh.get() << std::endl;
}