|
| 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 | + |
| 10 | +#include <iostream> |
| 11 | +#include <sstream> |
| 12 | +#include <cassert> |
| 13 | + |
| 14 | +// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp |
| 15 | +// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE. |
| 16 | + |
| 17 | +#include <msgpack.hpp> |
| 18 | + |
| 19 | +struct json_like_visitor : msgpack::v2::null_visitor { |
| 20 | + json_like_visitor(std::string& s):m_s(s) {} |
| 21 | + |
| 22 | + bool visit_nil() { |
| 23 | + m_s += "null"; |
| 24 | + return true; |
| 25 | + } |
| 26 | + bool visit_boolean(bool v) { |
| 27 | + if (v) m_s += "true"; |
| 28 | + else m_s += "false"; |
| 29 | + return true; |
| 30 | + } |
| 31 | + bool visit_positive_integer(uint64_t v) { |
| 32 | + std::stringstream ss; |
| 33 | + ss << v; |
| 34 | + m_s += ss.str(); |
| 35 | + return true; |
| 36 | + } |
| 37 | + bool visit_negative_integer(int64_t v) { |
| 38 | + std::stringstream ss; |
| 39 | + ss << v; |
| 40 | + m_s += ss.str(); |
| 41 | + return true; |
| 42 | + } |
| 43 | + bool visit_float(double v) { |
| 44 | + std::stringstream ss; |
| 45 | + ss << v; |
| 46 | + m_s += ss.str(); |
| 47 | + return true; |
| 48 | + } |
| 49 | + bool visit_str(const char* v, uint32_t size) { |
| 50 | + m_s += '"' + std::string(v, size) + '"'; |
| 51 | + return true; |
| 52 | + } |
| 53 | + bool start_array(uint32_t /*num_elements*/) { |
| 54 | + m_s += "["; |
| 55 | + return true; |
| 56 | + } |
| 57 | + bool end_array_item() { |
| 58 | + m_s += ","; |
| 59 | + return true; |
| 60 | + } |
| 61 | + bool end_array() { |
| 62 | + m_s.erase(m_s.size() - 1, 1); // remove the last ',' |
| 63 | + m_s += "]"; |
| 64 | + return true; |
| 65 | + } |
| 66 | + bool start_map(uint32_t /*num_kv_pairs*/) { |
| 67 | + m_s += "{"; |
| 68 | + return true; |
| 69 | + } |
| 70 | + bool end_map_key() { |
| 71 | + m_s += ":"; |
| 72 | + return true; |
| 73 | + } |
| 74 | + bool end_map_value() { |
| 75 | + m_s += ","; |
| 76 | + return true; |
| 77 | + } |
| 78 | + bool end_map() { |
| 79 | + m_s.erase(m_s.size() - 1, 1); // remove the last ',' |
| 80 | + m_s += "}"; |
| 81 | + return true; |
| 82 | + } |
| 83 | + void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { |
| 84 | + } |
| 85 | + void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { |
| 86 | + } |
| 87 | + std::string& m_s; |
| 88 | +}; |
| 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 | + |
| 102 | + std::string const& buf = ss.str(); |
| 103 | + auto it = buf.begin(); |
| 104 | + auto end = buf.end(); |
| 105 | + { |
| 106 | + std::string str; |
| 107 | + bool ret = msgpack::parse(it, end, json_like_visitor(str)); |
| 108 | + // it is updated here. |
| 109 | + assert(ret); |
| 110 | + std::cout << str << std::endl; |
| 111 | + } |
| 112 | + { |
| 113 | + std::string str; |
| 114 | + bool ret = msgpack::parse(it, end, json_like_visitor(str)); |
| 115 | + // it is updated here. |
| 116 | + assert(ret); |
| 117 | + std::cout << str << std::endl; |
| 118 | + } |
| 119 | +} |
0 commit comments