msgpack / msgpack-c

MessagePack implementation for C and C++ / msgpack.org[C/C++]
Other
3.02k stars 878 forks source link

How to convert msgpack::object to std::string or char *? #761

Closed nobody93 closed 5 years ago

nobody93 commented 5 years ago

Hi,

I can unpack the message to msgpack::object and display the json format in std::cout << object, but I need to convert msgpack::object json format to either std::string or char , then insert a string json data to mysql json field, I failed both cast to char or std::string, please advise.

Thank you.

redboltz commented 5 years ago

You can use std::stringstream instead of std::cout. And then, call std::stringstream::str() member function. http://www.cplusplus.com/reference/sstream/stringstream/str/

Here is an example:

#include <msgpack.hpp>
#include <sstream>
#include <iostream>

int main() {
    msgpack::zone z;
    auto obj = msgpack::object(std::make_tuple(1, 23.4, false), z);

    std::cout << obj << std::endl;

    std::stringstream ss;
    ss << obj;
    std::string str = ss.str();
    std::cout << str << std::endl;
}

https://wandbox.org/permlink/BtLUllLuAmbBBw4d

nobody93 commented 5 years ago

Thank you so much redboltz for your prompt advice.