Comments (2)
I write a converter from mpack_node_t to mpack_writer_t. It works well (except mpack_type_ext which I didn't use) and if currentNode->type is an array or a map, it will write all elements (key, value) by nested operations, which means finish function has been called automatically. It originate from (mpack_writer_t* writer, mpack_tag_t tag). And it only support change number to number.
example:
...//change node data externally
write_data_from_tree(writer, mpack_tree_root(tree));
void write_data_from_tree(mpack_writer_t* writer, mpack_node_t currentNode)
{
mpack_node_data_t *value = currentNode.data;
switch (value->type) {
case mpack_type_missing:
mpack_break("cannot write a missing value!");
mpack_writer_flag_error(writer, mpack_error_bug);
return;
case mpack_type_nil: mpack_write_nil (writer); return;
case mpack_type_bool: mpack_write_bool (writer, value->value.b); return;
case mpack_type_int: mpack_write_int (writer, value->value.i); return;
case mpack_type_uint: mpack_write_uint (writer, value->value.u); return;
case mpack_type_float:
#if MPACK_FLOAT
mpack_write_float
#else
mpack_write_raw_float
#endif
(writer, value->value.f);
return;
case mpack_type_double:
#if MPACK_DOUBLE
mpack_write_double
#else
mpack_write_raw_double
#endif
(writer, value->value.d);
return;
case mpack_type_str: mpack_write_str(writer, tree.data+value->value.offset, value->len); return;
case mpack_type_bin: mpack_write_bin(writer, tree.data+value->value.offset, value->len); return;
#if MPACK_EXTENSIONS
case mpack_type_ext:
#error data not include ext!
mpack_start_ext(writer, mpack_tag_ext_exttype(&value), mpack_tag_ext_length(&value));
return;
#endif
case mpack_type_array:
mpack_start_array(writer, value->len);
for(uint32_t i = 0; i < value->len; i++)
{
write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i)));
}
mpack_finish_array(writer);
return;
case mpack_type_map:
mpack_start_map(writer, value->len);
for(uint32_t i = 0; i < value->len; i++)
{
write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i*2)));
write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i*2+1)));
}
mpack_finish_array(writer);
return;
}
mpack_break("unrecognized type %i", (int)value.type);
mpack_writer_flag_error(writer, mpack_error_bug);
}
from mpack.
Sorry, the node API parse tree is meant to be immutable. It's heavily optimized for this. I would strongly recommend against changing anything in the node API parse tree.
Making it mutable would drastically alter its efficiency. I've done extensive benchmarking of this in the past. Jansson is an example of a JSON parser that builds a mutable tree and it is the slowest parser on the list.
I don't currently have code to load MessagePack into a mutable tree that can be modified and written out again. If you wanted to implement something like this, it's not very hard. You could make your own mutable tree node struct and use the reader API to build it and the writer API to write it out again. It would only take a hundred or so lines of code.
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 know the end of message pack in serial communication? HOT 1
- memory error on avr (arduino uno)
- File IO incompatible with custom context HOT 2
- Suggestions for warnings? HOT 3
- mpack crashes without a matching call to mpack_complete_* HOT 3
- Node API for read+write? HOT 1
- get nothing if prev node is nil HOT 3
- 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.