// MessagePack for C++ example // // Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include #include #include #include #include // msgpack.hpp is also ok #include 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(*it) & 0xff) << ' '; } std::cout << std::endl; } #include 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); }