문제

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