unpack.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2017 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 <iostream>
  10. #include <sstream>
  11. #include <cassert>
  12. // MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp
  13. // It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE.
  14. #include <msgpack.hpp>
  15. int main() {
  16. std::stringstream ss;
  17. std::map<std::string, std::vector<int>> v1 {
  18. { "ABC", { 1, 2, 3 } },
  19. { "DEFG", { 4, 5 } }
  20. };
  21. std::vector<std::string> v2 {
  22. "HIJ", "KLM", "NOP"
  23. };
  24. msgpack::pack(ss, v1);
  25. msgpack::pack(ss, v2);
  26. std::string const& buf = ss.str();
  27. auto it = buf.begin();
  28. auto end = buf.end();
  29. {
  30. auto oh = msgpack::unpack(it, end);
  31. // it is updated here.
  32. assert(v1 == (oh.get().as<std::map<std::string, std::vector<int>>>()));
  33. }
  34. {
  35. auto oh = msgpack::unpack(it, end);
  36. assert(v2 == oh.get().as<std::vector<std::string>>());
  37. }
  38. }