MessagePack C API [英] MessagePack C API
问题描述
在看C API为MessagePack,还有一些功能适当序列化(包)中的数据按照类型: msgpack_pack_uint8
, msgpack_pack_int32
...
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
, ...
似乎没有成为API中的等价的调用来解压数据。 msgpack_unpack_next
返回 msgpack_object
。这些对象仅具有类型粗粒度(最大的类型:int64类型,双,...),基于所述枚举包括在内。
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.
推荐答案
目前解包的时候,任何整数值总是存储在一个 msgpack_object
作为一个固定宽度64位整数(的int64_t
如果为负, uint64_t中
其他)。
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).
请参阅 CPP / src目录/ msgpack / object.h
的更多详细信息 msgpack_object
等,和 CPP / src目录/ msgpack / unpack.c
来看看msgpack如何处理拆包逻辑,例如:
See cpp/src/msgpack/object.h
for more details on msgpack_object
et al., and cpp/src/msgpack/unpack.c
to see how msgpack handles the unpacking logic, e.g.:
static inline int template_callback_int8(unpack_user* u,
int8_t d,
msgpack_object* o) {
if(d >= 0) {
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER; o->via.u64 = d;
return 0;
}
else {
o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o->via.i64 = d;
return 0;
}
}
这是因为在包装的时候,msgpack根据其价值选择动态最优化的方式来连接code整数,例如如果你使用 msgpack_pack_uint16
来收拾你的整数,则:
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:
- it will be saved in 1 byte if the value is in [0, 127],
- 2 bytes with
0xcc
as first byte if the value is in [128, 255], - 3 bytes with
0xcd
as first byte otherwise.
请参阅 msgpack_pack_real_uint16
从 CPP / src目录/ msgpack / pack_template.h
了解更多信息。
See msgpack_pack_real_uint16
from cpp/src/msgpack/pack_template.h
for more details.
在解压时的时间换句话说,msgpack使用一个足够大的正或负(测试,如果 obj.type
是 MSGPACK_OBJECT_POSITIVE_INTEGER
或 MSGPACK_OBJECT_NEGATIVE_INTEGER
)举行任何整数值。所以这是由你来:
In other words at unpack time, msgpack uses a large enough positive or negative (test if obj.type
is MSGPACK_OBJECT_POSITIVE_INTEGER
or MSGPACK_OBJECT_NEGATIVE_INTEGER
) to hold any integer value. So it's up to you to:
- 投,如果你总是可以假设值将不会溢出,你投的类型,
- 或,动态地检查(用掩模)如果该值不为您的接收器类型, 足够大
- 或总是使用
的int64_t
或uint64_t中
。
- cast if you can always assume that values will never overflow your cast type,
- or, check dynamically (with a mask) if the value is not large enough for your receiver type,
- or, always use an
int64_t
oruint64_t
.
最后,C测试套件( msgpack / CPP /测试/ msgpackc_test.cpp
)可能会有所帮助浏览code样品。
At last, the C test suite (msgpack/cpp/test/msgpackc_test.cpp
) might be helpful to browse code samples.
这篇关于MessagePack C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!