simple.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include <msgpack.hpp>
  10. #include <string>
  11. #include <iostream>
  12. #include <sstream>
  13. int main(void)
  14. {
  15. msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
  16. // serialize the object into the buffer.
  17. // any classes that implements write(const char*,size_t) can be a buffer.
  18. std::stringstream buffer;
  19. msgpack::pack(buffer, src);
  20. // send the buffer ...
  21. buffer.seekg(0);
  22. // deserialize the buffer into msgpack::object instance.
  23. std::string str(buffer.str());
  24. msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());
  25. // deserialized object is valid during the msgpack::object_handle instance alive.
  26. msgpack::object deserialized = oh.get();
  27. // msgpack::object supports ostream.
  28. std::cout << deserialized << std::endl;
  29. // convert msgpack::object instance into the original type.
  30. // if the type is mismatched, it throws msgpack::type_error exception.
  31. msgpack::type::tuple<int, bool, std::string> dst;
  32. deserialized.convert(dst);
  33. return 0;
  34. }