parse.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. struct json_like_visitor : msgpack::v2::null_visitor {
  16. json_like_visitor(std::string& s):m_s(s) {}
  17. bool visit_nil() {
  18. m_s += "null";
  19. return true;
  20. }
  21. bool visit_boolean(bool v) {
  22. if (v) m_s += "true";
  23. else m_s += "false";
  24. return true;
  25. }
  26. bool visit_positive_integer(uint64_t v) {
  27. std::stringstream ss;
  28. ss << v;
  29. m_s += ss.str();
  30. return true;
  31. }
  32. bool visit_negative_integer(int64_t v) {
  33. std::stringstream ss;
  34. ss << v;
  35. m_s += ss.str();
  36. return true;
  37. }
  38. bool visit_float32(float v) {
  39. std::stringstream ss;
  40. ss << v;
  41. m_s += ss.str();
  42. return true;
  43. }
  44. bool visit_float64(double v) {
  45. std::stringstream ss;
  46. ss << v;
  47. m_s += ss.str();
  48. return true;
  49. }
  50. bool visit_str(const char* v, uint32_t size) {
  51. m_s += '"' + std::string(v, size) + '"';
  52. return true;
  53. }
  54. bool start_array(uint32_t /*num_elements*/) {
  55. m_s += "[";
  56. return true;
  57. }
  58. bool end_array_item() {
  59. m_s += ",";
  60. return true;
  61. }
  62. bool end_array() {
  63. m_s.erase(m_s.size() - 1, 1); // remove the last ','
  64. m_s += "]";
  65. return true;
  66. }
  67. bool start_map(uint32_t /*num_kv_pairs*/) {
  68. m_s += "{";
  69. return true;
  70. }
  71. bool end_map_key() {
  72. m_s += ":";
  73. return true;
  74. }
  75. bool end_map_value() {
  76. m_s += ",";
  77. return true;
  78. }
  79. bool end_map() {
  80. m_s.erase(m_s.size() - 1, 1); // remove the last ','
  81. m_s += "}";
  82. return true;
  83. }
  84. void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
  85. }
  86. void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
  87. }
  88. std::string& m_s;
  89. };
  90. int main() {
  91. std::stringstream ss;
  92. std::map<std::string, std::vector<int>> v1 {
  93. { "ABC", { 1, 2, 3 } },
  94. { "DEFG", { 4, 5 } }
  95. };
  96. std::vector<std::string> v2 {
  97. "HIJ", "KLM", "NOP"
  98. };
  99. msgpack::pack(ss, v1);
  100. msgpack::pack(ss, v2);
  101. std::string const& buf = ss.str();
  102. auto it = buf.begin();
  103. auto end = buf.end();
  104. {
  105. std::string str;
  106. bool ret = msgpack::parse(it, end, json_like_visitor(str));
  107. // it is updated here.
  108. assert(ret);
  109. std::cout << str << std::endl;
  110. }
  111. {
  112. std::string str;
  113. bool ret = msgpack::parse(it, end, json_like_visitor(str));
  114. // it is updated here.
  115. assert(ret);
  116. std::cout << str << std::endl;
  117. }
  118. }