Вопрос

In the C API for MsgPack, why are keys (for example the key field of msgpack_object_kv) of type msgpack_object?

https://github.com/msgpack/msgpack-c/blob/master/src/msgpack/object.h

Why aren't they just msgpack_object_raw? Can they be any type?

Это было полезно?

Решение

Can they be any type?

Yes. And that's precisely why keys are represented as msgpack_object-s.

If you look at the maps format specification you can see that maps are defined by N = number of pairs within the map where:

odd elements are key and next element of the key is its associate value

So you are free to use whatever kind of object for keys.

Here's an example (with a map made of 2 pairs) from test/msgpackc_test.cpp:

unsigned int map_size = 2;
/* ... */
msgpack_pack_map(&pk, map_size);
msgpack_pack_true(&pk);
msgpack_pack_false(&pk);
msgpack_pack_int(&pk, 10);
msgpack_pack_int(&pk, -10);

As you can see the first key is a MSGPACK_OBJECT_BOOLEAN and the second one is a MSGPACK_OBJECT_POSITIVE_INTEGER.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top