You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to convert EXT(type:1,size=8) data in cpp #1072
When i deserilize message, i have map which contains either int/bool/string/EXT(type:1,size8).
to convert EXT(type:1,size8) which initially contains int from python.
I parse it as follows.
void on_incoming_message(const AMQP::Message &message, uint64_t deliveryTag, bool redelivered)
//void on_incoming_message()
{
std::string msg(message.body(), message.bodySize());
msgpack::object_handle oh =
msgpack::unpack(msg.data(), msg.size());
// deserialized object is valid during the msgpack::object_handle instance is alive.
msgpack::object deserialized = oh.get();
cout << deserialized.type << "------------ type >>>" << endl;
std::cout << deserialized << std::endl;
// convert msgpack::object instance into the original type.
// if the type is mismatched, it throws msgpack::type_error exception.
std::unordered_map<std::string,std::string> output;
if (deserialized.type == msgpack::type::MAP) {
msgpack::object_map& map = deserialized.via.map;
output = convertObjectMap(deserialized.via.map);
}
print_event(output);
// or create the new instance
//msgpack::type::tuple<int, bool, std::string> dst2 =
std::unordered_map<std::string, std::string> convertObjectMap(const msgpack::object_map& objectMap)
{
std::unordered_map<std::string, std::string> result;
for (const auto& pair : objectMap)
{
std::string key, value;
// Convert the key to std::string
pair.key.convert(key);
// Convert the value to std::string
value = convertToString(pair.val);
// Store the key-value pair in the result map
result[key] = value;
}
return result;
}
std::string convertToString(const msgpack::object& obj) {
{
if (obj.type == msgpack::type::EXT) {
std::string msg(obj.via.ext.data(), obj.via.ext.size);
auto abc =msgpack::unpack(msg.data(),msg.size());
uint64_t intValue;
abc.get().convert(intValue);
value = std::to_string(intValue);
}
}
it is unable to parse value.
Im new to msgpack and cpp that could a reason of such simple questions
I recommend that create a simple code (no MAP, no AMQP).
That is focused on msgpack EXT only like the test code I mentioned.
And confirm the simple code works well, and then apply your project.
Activity
redboltz commentedon May 25, 2023
When msgpack-c(C++) receives EXT format family MessagePack formatted byte stream, msgpack-c creates
msgpack::objectand its type ismsgpack::type::EXT.See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_object
You can access
msgpack::objectdirectly as follows:msgpack-c/test/msgpack_basic.cpp
Line 328 in ac062e2
Or you can use
msgpack::type::extormsgpack::type::ext_refhelper classes as follows:msgpack-c/test/msgpack_basic.cpp
Lines 590 to 608 in ac062e2
Avalanchecoder commentedon May 25, 2023
redboltz commentedon May 25, 2023
I recommend that create a simple code (no MAP, no AMQP).
That is focused on msgpack EXT only like the test code I mentioned.
And confirm the simple code works well, and then apply your project.