Closed wukan1986 closed 2 years ago
same problem with expect api
mpack_reader_t reader;
mpack_expect_map_match(&reader, 5);
mpack_expect_cstr_match(&reader, "A");
char buf1[128] = { 0 };
mpack_expect_utf8_cstr(&reader, buf1, sizeof(buf1));
mpack_expect_cstr_match(&reader, "B");
char buf2[128] = { 0 };
mpack_expect_utf8_cstr(&reader, buf2, sizeof(buf2));
// ......
mpack_done_map(&reader);
This behaviour may be a bit counter-intuitive in your example but it is intentional. When you call mpack_node_strlen()
on a node, that node is expected to be a string. If it isn't, it means the data must be corrupt: the whole tree is placed in an error state and all subsequent calls on the tree return safe nil/zero values. This guarantees that the data matches the hardcoded schema you are expecting and your app won't crash if it doesn't. If you check the tree for errors after parsing you would get mpack_error_type
because there was a nil node where a str was expected. The same happens with mpack_expect_utf8_cstr()
.
If you want to avoid flagging an error, call mpack_node_is_nil()
first.
for (size_t i = 0; i < 5; ++i)
{
char key[] = "A";
key[0] += i;
auto node_1 = mpack_node_map_cstr(root, key);
if (mpack_node_is_nil(node_1)) {
std::cout << "NIL" << std::endl;
} else {
auto len_1 = mpack_node_strlen(node_1);
auto str_1 = mpack_node_str(node_1);
std::cout << std::string(str_1, len_1) << std::endl;
}
}
This prints:
11
22
NIL
44
55
I got it Thank you very mush
Hi, Thank you for your work.
this is my code:
the output is:
I expect is