Comments (3)
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);
from mpack.
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
from mpack.
I got it
Thank you very mush
from mpack.
Related Issues (20)
- Stream reader with manual memory management? HOT 2
- comparison with libmpack HOT 1
- Warning about incompatible int types in mpack_snprintf calls HOT 1
- ESP32-C3 rebooting when executing functions from the MPack library
- Stream parser that doesn't buffer the entire message
- tools/unit.sh help - does not work
- How to use the protocol on both client side and server side HOT 1
- Copy element from node API to write API HOT 1
- New release? HOT 3
- Suggestions for warnings? HOT 3
- mpack crashes without a matching call to mpack_complete_* HOT 3
- Node API for read+write? HOT 1
- How to change the data with node API? Or how to write fast when I know the data offset or giving node? HOT 2
- Make it possible to use a dynamic buffer with mpack writer HOT 2
- builder page allocation leak and segfault HOT 6
- Missing Symbolic Link HOT 1
- Should NULL data be allowed for mpack_write_str() with count of 0? HOT 1
- Error handler segfault HOT 1
- How can I get it header only? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mpack.