In looking at the C API for MessagePack, there are a number of functions to appropriately serialize (pack) the data according to type: msgpack_pack_uint8
, msgpack_pack_int32
, ...
There doesn't seem to be the equivalent call in the API to unpack the data. msgpack_unpack_next
returns a msgpack_object
. These objects only have coarse granularity of types (the largest of the type: int64, double, ...), based on the enums included.
Am I missing something here? Is the expectation that the coarse object be used and then cast?
How should unpacking be done properly?
Furthermore, is there any good documentation or usage examples? The ones on the website are trivial.
At unpack time, any integer value is always stored within a
msgpack_object
as a fixed-width 64-bit integer (int64_t
if negative,uint64_t
otherwise).See
cpp/src/msgpack/object.h
for more details onmsgpack_object
et al., andcpp/src/msgpack/unpack.c
to see how msgpack handles the unpacking logic, e.g.:This is because at pack time, msgpack chooses dynamically the most optimal way to encode an integer according to its value, e.g. if you use
msgpack_pack_uint16
to pack your integer then:0xcc
as first byte if the value is in [128, 255],0xcd
as first byte otherwise.See
msgpack_pack_real_uint16
fromcpp/src/msgpack/pack_template.h
for more details.In other words at unpack time, msgpack uses a large enough positive or negative (test if
obj.type
isMSGPACK_OBJECT_POSITIVE_INTEGER
orMSGPACK_OBJECT_NEGATIVE_INTEGER
) to hold any integer value. So it's up to you to:int64_t
oruint64_t
.At last, the C test suite (
msgpack/cpp/test/msgpackc_test.cpp
) might be helpful to browse code samples.