1// MessagePack for C++ example 2// 3// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi 4// 5// Distributed under the Boost Software License, Version 1.0. 6// (See accompanying file LICENSE_1_0.txt or copy at 7// http://www.boost.org/LICENSE_1_0.txt) 8// 9 10#include <msgpack.hpp> 11#include <string> 12#include <iostream> 13#include <sstream> 14 15int main(void) 16{ 17 msgpack::type::tuple<int, bool, std::string> src(1, true, "example"); 18 19 // serialize the object into the buffer. 20 // any classes that implements write(const char*,size_t) can be a buffer. 21 std::stringstream buffer; 22 msgpack::pack(buffer, src); 23 24 // send the buffer ... 25 buffer.seekg(0); 26 27 // deserialize the buffer into msgpack::object instance. 28 std::string str(buffer.str()); 29 30 msgpack::unpacked result; 31 32 msgpack::unpack(result, str.data(), str.size()); 33 34 // deserialized object is valid during the msgpack::unpacked instance alive. 35 msgpack::object deserialized = result.get(); 36 37 // msgpack::object supports ostream. 38 std::cout << deserialized << std::endl; 39 40 // convert msgpack::object instance into the original type. 41 // if the type is mismatched, it throws msgpack::type_error exception. 42 msgpack::type::tuple<int, bool, std::string> dst; 43 deserialized.convert(dst); 44 45 return 0; 46} 47