mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-24 17:18:33 +02:00
54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#include <string>
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <cassert>
|
|
|
|
#include <msgpack.hpp>
|
|
|
|
|
|
class my_class {
|
|
public:
|
|
my_class() {} // When you want to convert from msgpack::object to my_class,
|
|
// my_class should be default constractible.
|
|
my_class(std::string const& name, int age):name_(name), age_(age) {}
|
|
|
|
friend bool operator==(my_class const& lhs, my_class const& rhs) {
|
|
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
|
|
}
|
|
|
|
private:
|
|
std::string name_;
|
|
int age_;
|
|
|
|
public:
|
|
MSGPACK_DEFINE(name_, age_);
|
|
};
|
|
|
|
void print(std::string const& buf) {
|
|
for (std::string::const_iterator it = buf.begin(), end = buf.end();
|
|
it != end;
|
|
++it) {
|
|
std::cout
|
|
<< std::setw(2)
|
|
<< std::hex
|
|
<< std::setfill('0')
|
|
<< (static_cast<int>(*it) & 0xff)
|
|
<< ' ';
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
int main() {
|
|
my_class my("John Smith", 42);
|
|
std::stringstream ss;
|
|
msgpack::pack(ss, my);
|
|
|
|
print(ss.str());
|
|
|
|
msgpack::unpacked unp;
|
|
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
|
msgpack::object obj = unp.get();
|
|
assert(obj.as<my_class>() == my);
|
|
}
|