compared with
Current by Lukas Vacek
on Mar 14, 2013 19:53.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (1)

View Page History
}
{code}

h3. Streaming into an array or map

{code}
#include <msgpack.hpp>
#include <iostream>
#include <string>

int main(void) {
// serializes multiple objects into one message containing an array using msgpack::packer.
msgpack::sbuffer buffer;

msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack_array(3)
pk.pack(std::string("Log message ... 1"));
pk.pack(std::string("Log message ... 2"));
pk.pack(std::string("Log message ... 3"));

// serializes multiple objects into one message containing a map using msgpack::packer.
msgpack::sbuffer buffer2;

msgpack::packer<msgpack::sbuffer> pk2(&buffer2);
pk2.pack_map(2)
pk2.pack(std::string("x"));
pk2.pack(3);
pk2.pack(std::string("y));
pk2.pack(3.4321);

}
{code}


h2. User-defined classes

You can use serialize/deserializes user-defined classes using MSGPACK_DEFINE macro.

{code}
#include <msgpack.hpp>
#include <vector>
#include <string>

class myclass {
private:
std::string m_str;
std::vector<int> m_vec;
public:
MSGPACK_DEFINE(m_str, m_vec);
};

int main(void) {
std::vector<myclass> vec;
// add some elements into vec...

// you can serialize myclass directly
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, vec);

msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());

msgpack::object obj = msg.get();

// you can convert object to myclass directly
std::vector<myclass> rvec;
obj.convert(&rvec);
}
{code}