msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

how to convert EXT(type:1,size=8) data in cpp

Open Avalanchecoder opened this issue 2 years ago • 3 comments

{"signal_id":2801684810234,"type":"SIGNAL","signaltype":"NEW_ENTRY","exit_time":datetime.time(15,55),"remarks":26} elif isinstance(obj, datetime.time): # always encode in the 8-byte form data = datetime.datetime.combine( datetime.datetime.today(), obj) data = int((obj.hour * 60 * 60 + obj.minute * 60 + obj.second) * 1e6 + obj.microsecond)

    return KanhojiExtType(1, data.to_bytes(8, byteorder='big'))

this is my encoding logic I am sending this from python. datetime format which in convert in "int" with type code 1

when i deserialise msgpack in cpp {"signal_id":2801684810234,"type":"SIGNAL","signaltype":"NEW_ENTRY","exit_time":"EXT(type:1,size:8)","remarks":26}

could you help me to deserialising this field "exit_time":"EXT(type:1,size:8)" ?

Avalanchecoder avatar May 24 '23 13:05 Avalanchecoder

When msgpack-c(C++) receives EXT format family MessagePack formatted byte stream, msgpack-c creates msgpack::object and its type is msgpack::type::EXT. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_object

You can access msgpack::object directly as follows: https://github.com/msgpack/msgpack-c/blob/ac062e28cbf80db345800b6e113dea8ab0055499/test/msgpack_basic.cpp#L328

Or you can use msgpack::type::ext or msgpack::type::ext_ref helper classes as follows: https://github.com/msgpack/msgpack-c/blob/ac062e28cbf80db345800b6e113dea8ab0055499/test/msgpack_basic.cpp#L590-L608

redboltz avatar May 25 '23 02:05 redboltz

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


From: Takatoshi Kondo @.> Sent: 25 May 2023 07:39 To: msgpack/msgpack-c @.> Cc: Avalanchecoder @.>; Author @.> Subject: Re: [msgpack/msgpack-c] how to convert EXT(type:1,size=8) data in cpp (Issue #1072)

When msgpack-c(C++) receives EXT format family MessagePack formatted byte stream, msgpack-c creates msgpack::object and its type is msgpack::type::EXT. See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_object

You can access msgpack::object directly as follows: https://github.com/msgpack/msgpack-c/blob/ac062e28cbf80db345800b6e113dea8ab0055499/test/msgpack_basic.cpp#L328

Or you can use msgpack::type::ext or msgpack::type::ext_ref helper classes as follows: https://github.com/msgpack/msgpack-c/blob/ac062e28cbf80db345800b6e113dea8ab0055499/test/msgpack_basic.cpp#L590-L608

— Reply to this email directly, view it on GitHubhttps://github.com/msgpack/msgpack-c/issues/1072#issuecomment-1562158781, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AKQHHHVFEJLZD6FWYSNCQQLXH25NTANCNFSM6AAAAAAYNNRP4M. You are receiving this because you authored the thread.Message ID: @.***>

Avalanchecoder avatar May 25 '23 05:05 Avalanchecoder

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.

redboltz avatar May 25 '23 05:05 redboltz