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_objectas a fixed-width 64-bit integer (int64_tif negative,uint64_totherwise).See
cpp/src/msgpack/object.hfor more details onmsgpack_objectet al., andcpp/src/msgpack/unpack.cto 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_uint16to pack your integer then:0xccas first byte if the value is in [128, 255],0xcdas first byte otherwise.See
msgpack_pack_real_uint16fromcpp/src/msgpack/pack_template.hfor more details.In other words at unpack time, msgpack uses a large enough positive or negative (test if
obj.typeisMSGPACK_OBJECT_POSITIVE_INTEGERorMSGPACK_OBJECT_NEGATIVE_INTEGER) to hold any integer value. So it’s up to you to:int64_toruint64_t.At last, the C test suite (
msgpack/cpp/test/msgpackc_test.cpp) might be helpful to browse code samples.